2014年7月13日 星期日

[RESOLVED] Access ApplicationUser property from site.master


Hi:


I have a webforms application using ASP.NET Identity 2, and I'm trying to show a custom property from ApplicationUser instead of Context.User.Identiy.GetUsername() in the login menu on the NavBar of the pages that are generated by site.master.


I'd greatly appreciate some hints how to get to my custom properties of the ApplicationUser object from wihin Site.Master to do this.



THANKS


Cheers -alex







Hi,


According to your description, you want to display the custom property for ApplicationUser. So far as I know, we need to take your custom properties that are already being passed in the SignInAsync() method via the user parameter and save them as "claims"
on the identity object.


private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
identity.AddClaim(new Claim("FullName", user.FullName));
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

Second, you need to write an extention class for the User object.


There is a similar thread, please refer to the link below:


http://forums.asp.net/t/1957500.aspx?How+to+access+custom+Identity+or+ApplicationUser+properties+


Hope it's useful for you.


Best Regards,


Michelle Ge



Thanks Michelle, I'll give that a shot. I think the extension is most likely the way to go, as I already have the properties in the local user database, just need to find a way to get to it from the site.master.


Cheers -alex




Michelle:



THANKS so much. Works perfectly!



Cheers -alex



沒有留言:

張貼留言