Click here to Skip to main content
13,667,677 members
Rate this:
 
Please Sign up or sign in to vote.
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 16-Feb-17 9:59am
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The User property returns an IPrincipal instance. To access the claims, you'll need to cast that to a ClaimsPrincipal.
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?);
    }
}
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180820.1 | Last Updated 16 Feb 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100