New to OWIN authentication and finding it hard to create my own owin claim types.
Heres the thing. I need to add custom claims like "GroupID" so i can easily access it on the different pages.
I did something like this in my Login
public ActionResult Login(LoginViewModel model, string returnUrl)
{
UserViewModel userModel = new UserViewModel();
if (!ModelState.IsValid)
{
return View(model);
}
if(CommonHelper.ValidateADUser(model.Username,model.Password))
{
UserViewModel curUser = userModel.GetUserDetails(model.Username);
if (curUser != null)
{
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.WindowsAccountName, curUser.Username));
claims.Add(new Claim(ClaimTypes.Name,curUser.Fullname));
claims.Add(new Claim(ClaimTypes.Role, ""));
claims.Add(new Claim("GroupID", curUser.UserGroupID.ToString()));
var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
var ctx = Request.GetOwinContext();
AuthenticationManager.SignIn(id);
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "Invalid login attempt.");
}
return View(model);
}
In my login partial I tried to get the value by doing this
public ActionResult _LoginPartial()
{
var identity = (ClaimsIdentity)User.Identity;
TempData["curUserFullName"] = identity.FindFirst(ClaimTypes.Name).Value;
string s= identity.FindFirst("GroupID").Value;
return PartialView();
}
I can get the username and full name with no problem but group id causes an object null error.
Hoping someone could nudge me to the correct answer.
claims.Add(new Claim("MyTestClaim", "Test Value"));
Can you retrieve it?AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, id);
. I've got pretty much the same set up for one of my projects and have this working fine.AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);