Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

i can login wit claims but i can't access claim values. i just accessing "name" value

public ActionResult Login(UserModel model)
        {
                var userCheck = db.User.Where(u => u.Mail == model.Mail && u.Password == model.Password);
                foreach (var item in userCheck)
                {

                    var identity = new ClaimsIdentity(new[] {
                new Claim(ClaimTypes.Sid, item.UserID.ToString()),
                new Claim(ClaimTypes.Name, item.Name),
                new Claim(ClaimTypes.Surname, item.Surname),
                new Claim(ClaimTypes.Email, item.Mail)
            },
                               "ApplicationCookie");

                    var ctx = Request.GetOwinContext();
                    var authManager = ctx.Authentication;

                    authManager.SignIn(identity);
               
               
                return Redirect(GetRedirectUrl(model.ReturnUrl));
                }
             
            return View(model);
    }


Global.asax

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);


            AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Sid;
            AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Name;
            AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Surname;
            AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Email;

        }



Image - TinyPic - Free Image Hosting, Photo Sharing & Video Hosting[^]

Image - TinyPic - Free Image Hosting, Photo Sharing & Video Hosting[^]


how can i access other claim values ?

What I have tried:

...........................................................
Posted
Updated 16-Feb-17 10:32am
Comments
Richard Deeming 16-Feb-17 16:19pm    
It looks like you're storing passwords in plain text. Don't do that.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

If you use the built-in Identity[^] system, it will take care of this sort of thing for you.

1 solution

The User property returns an IPrincipal instance. To access the claims, you'll need to cast that to a ClaimsPrincipal.
C#
public static class ClaimsExtensions
{
    public static Claim FindClaim(this IPrincipal user, string claimType)
    {
        if (user == null) throw new ArgumentNullException(nameof(user));
        if (string.IsNullOrEmpty(claimType)) throw new ArgumentNullException(nameof(claimType));
        
        var claimsPrincipal = user as ClaimsPrincipal;
        return claimsPrincipal?.FindFirst(claimType);
    }
    
    public static string GetEmail(this IPrincipal user)
    {
        if (user == null) throw new ArgumentNullException(nameof(user));
        return FindClaim(user, ClaimTypes.Email)?.Value;
    }
    
    public static string GetSurname(this IPrincipal user)
    {
        if (user == null) throw new ArgumentNullException(nameof(user));
        return FindClaim(user, ClaimTypes.Surname)?.Value;
    }
    
    public static int? GetUserId(this IPrincipal user)
    {
        if (user == null) throw new ArgumentNullException(nameof(user));
        
        string value = FindClaim(user, ClaimTypes.Sid)?.Value;
        if (string.IsNullOrEmpty(value)) return default(int?);
        
        int result;
        return int.TryParse(value, out result) ? result : default(int?);
    }
}
 
Share this answer
 
Comments
Maria Prince 13-Jul-18 3:44am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900