c# - How to Add another Propertys to User.Identity From table AspNetUsers in identity 2.2.1 -


i add new property asp.net identity 2.2.1 (aspnetusers table) code first

 public class applicationuser : identityuser     {         public string accesstoken { get; set; }          public string fullname { get; set; }          public string profilepicture { get; set; }           public async task<claimsidentity> generateuseridentityasync(usermanager<applicationuser> manager)         {             // note authenticationtype must match 1 defined in cookieauthenticationoptions.authenticationtype             var useridentity = await manager.createidentityasync(this, defaultauthenticationtypes.applicationcookie);             // add custom user claims here              return useridentity;         }     } 

ok , want call profile picture such code : user.identity.profilepicture;

the solution :

you need create own classes implement iidentity , iprincipal. assign them in global.asax in onpostauthenticate.

but dont know how !! how create own classes implement iidentity , iprincipal. assign them in global.asax in onpostauthenticate. .

you have 2 option (at least). first, set additional property claim when user logs in read property claim each time need. second, each time need property read storage (db). while recommend claim based approach, faster, show both way using extension methods.

first approach:

put own claim in generateuseridentityasync method this:

public class applicationuser : identityuser {     // code here      public async task<claimsidentity> generateuseridentityasync(usermanager<applicationuser> manager)     {         var useridentity = await manager.createidentityasync(this, defaultauthenticationtypes.applicationcookie);         useridentity.addclaim(new claim("profilepicture", this.profilepicture));         return useridentity;     } } 

then write extension method read claim this:

public static class identityhelper {     public static string getprofilepicture(this iidentity identity)     {         var claimident = identity claimsidentity;         return claimident != null             && claimident.hasclaim(c => c.type == "profilepicture")             ? claimident.findfirst("profilepicture").value             : string.empty;     } } 

now use extension method this:

var pic = user.identity.getprofilepicture(); 

second approach:

if prefer fresh data instead of cashed 1 in claim, write extension method property user manager:

public static class identityhelper {     public static string getfreshprofilepicture(this iidentity identity)     {         var usermanager = httpcontext.current.getowincontext().getusermanager<applicationusermanager>();         return usermanager.findbyid(identity.getuserid()).profilepicture;     } } 

now use this:

var pic = user.identity.getfreshprofilepicture(); 

also don't forget add relevant namespaces:

using system.security.claims; using system.security.principal; using system.web; using microsoft.aspnet.identity.owin; using microsoft.aspnet.identity; 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -