Hi all.
I'm using .NET 4.5 MVC. I'm new to MVC but I don't think that's the problem.
I'm stumped. I have a user that has a custom claim, FullName, and I don't know how to access it after signing in. I can verify that the claim exists in the AspNetUserClaims table. The user is authenticated. I'm not on the Login screen, nor even the screen after that. I'm in the middle of a normal session.
I'd like to use the FullName claim in the MVC _LoginPartial.cshtml file so it says "Welcome, [FullName]." I've done a ton of googling around and all the methods for accessing claims boils down to these two methods, which both do not work for me.
//Method 1 var identity = (System.Security.Claims.ClaimsIdentity)Context.User.Identity; var claim1 = identity.Claims.FirstOrDefault(c => c.Type == "FullName"); //Method 2 var principal = System.Security.Claims.ClaimsPrincipal.Current; System.Security.Claims.Claim claim2 = principal.FindFirst("FullName");
In the above examples, both claim1 and claim2 evaluate to null.
I found this blog which suggests that what I'm trying to do is impossible. Is that so?
The whole reason I'm using claims is so I can store a little bit of extra information on a user. Historically I"d add some columns to the aspnet_Membership table and I'd have to customize the Membership classes to account for the new columns, so claims looked a lot easier, but I've wasted so much time trying to get them to work! Any help is appreciated!
EDIT: This code DOES work during the login process, in AccountController.cs, Login(model, returnUrl):
//Method 3 var user = UserManager.FindByName(model.UserName); string fn = user.Claims.FirstOrDefault(c => c.ClaimType == "FullName").ClaimValue;
So, that's another way I know the claim exists. I do not know how to use this in _LoginPartial.cshtml because I do not know how to access UserManager there.