Add Custom Claim Types

35,405

Solution 1

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.

Solution 2

You can use type and value like this.

claims.Add(new Claim(type: "GroupID", value: curUser.UserGroupID.ToString()));
Share:
35,405
TheProvost
Author by

TheProvost

A professional experienced in the IT industry in the aspects of Software Design and Development, Technical Support, Software Deployment Capable of blending into varied environments of the broad exciting world of IT Flexible and excited to learn more technologies if needed

Updated on July 30, 2022

Comments

  • TheProvost
    TheProvost over 1 year

    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.

  • TheProvost
    TheProvost over 8 years
    What the!!!! Rookie mistake. I've used different browsers so I thought cookies was not the issue. Thanks!!!
  • DarkMakukudo
    DarkMakukudo over 8 years
    Its because otang monggos
  • matt.
    matt. over 8 years
    That hotkey is extremely helpful!