Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Custom Claim Types

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.

like image 315
TheProvost Avatar asked Oct 12 '15 06:10

TheProvost


People also ask

What are claim types?

The six most common types of claim are: fact, definition, value, cause, comparison, and policy. Being able to identify these types of claim in other people's arguments can help students better craft their own.

What is custom claim?

Defining roles via Firebase Functions on user creation. In this example, custom claims are set on a user on creation using Cloud Functions. Custom claims can be added using Cloud Functions, and propagated immediately with Realtime Database. The function is called only on signup using an onCreate trigger.

What are claims in ad?

A claim typically consists of an Active Directory user attribute, such as the user principal name (UPN) or email address. A security token bundles the set of claims about a particular user in the form of a Security Assertion Markup Language (SAML) assertion.


1 Answers

As I've checked your Code there are no problem in it.

Here's what I want you to do:

Check if the cache was clear. If your cache was not cleared you can clear it like this: CTRL+SHIFT+DELETE

It's because during your login, it will save in cookies and it was not clear, and it happens that the new claim was not save in the cookies.

Or try to check if your UserGroupID has a value.

like image 172
kwingkwingko Avatar answered Oct 05 '22 17:10

kwingkwingko