如何另一个Propertys加入User.Identity从表AspNetUsers身份2.2.1 [英] How to Add another Propertys to User.Identity From table AspNetUsers in identity 2.2.1
问题描述
我添加了一些新的属性,asp.net身份2.2.1(AspNetUsers表)code首先
i Add some new property to 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 the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
好了,现在我要打电话头像像这样code:
User.Identity.ProfilePicture;
Ok , Now I want to Call Profile Picture such as this code : User.Identity.ProfilePicture;
的解决方案是:
您需要创建实现IIdentity的自己的类和
IPrincipal的。然后将它们分配在你的Global.asax
OnPostAuthenticate。
You need to create your own classes that implement IIdentity and IPrincipal. Then assign them in your global.asax in OnPostAuthenticate.
但我不知道如何做到这一点!如何创建实现的IIdentity和IPrincipal的我自己的类。然后在OnPostAuthenticate您的Global.asax分配。
谢谢 。
But I dont know How to do this !! how to create my own classes that implement IIdentity and IPrincipal. Then assign them in your global.asax in OnPostAuthenticate. Thanks .
推荐答案
您有2个选项(至少)。首先,设置你的附加属性为索赔时,然后在用户登录读取索赔每次需要时间。其次,每个你所需要的属性时从存储(DB)读取。虽然我建议索赔为基础的方法,这是更快,我将使用扩展方法告诉你这两个办法。
You have 2 option (at least). First, set your additional property as a claim when user logs in then read the property from the claim each time you need. Second, each time you need the property read it from the storage (DB). While I recommend the claim based approach, which is faster, I will show you both way by using extension methods.
第一种方法:
在放像这样 GenerateUserIdentityAsync
方法,你自己的要求:
Put your own claim in the GenerateUserIdentityAsync
method like this:
public class ApplicationUser : IdentityUser
{
// some 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 a extension method to easily read the claim like this:
public static class IdentityHelper
{
public static string GetProfilePicture(this IIdentity identity)
{
var claimIdent = identity as ClaimsIdentity;
return claimIdent != null
&& claimIdent.HasClaim(c => c.Type == "ProfilePicture")
? claimIdent.FindFirst("ProfilePicture").Value
: string.Empty;
}
}
现在,你可以很容易地使用你的扩展方法是这样的:
Now you could easily use your extension method like this:
var pic = User.Identity.GetProfilePicture();
第二种方法:
如果您preFER新的数据,而不是要求兑现一个,你可以编写另一个扩展方法来从用户管理器获取属性:
If you prefer fresh data instead of cashed one in the claim, you could write another extension method to get the property from 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;
}
}
现在只需使用这样的:
var pic = User.Identity.GetFreshProfilePicture();
另外,不要忘了添加相关的命名空间:
Also don't forget to add relevant namespaces:
using System.Security.Claims;
using System.Security.Principal;
using System.Web;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity;
这篇关于如何另一个Propertys加入User.Identity从表AspNetUsers身份2.2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!