你能扩展HttpContext.Current.User.Identity属性吗?

本文关键字:Identity 属性 User Current 扩展 HttpContext | 更新日期: 2023-09-27 18:07:40

是否有办法覆盖HttpContext.Current.User.Identity添加另一个属性(屏幕名称)?

我的应用程序使用Identity,我留下了唯一的身份作为email。我将用户数据(如姓/名)存储在单独的"Profile"表中。是否有一种方法将此信息存储在HttpContext.Current中的某个地方?

它不一定需要在User之内。我有一个搜索,并注意到有一个HttpContext.Current.ProfileBase。不知道如何使用它-我真的不想要所有多余的东西,基础来了。

你能扩展HttpContext.Current.User.Identity属性吗?

如果您使用的是Asp。Net Identity,那么这就很容易做索赔了。

在您的SignInAsync方法中(或者,无论您在哪里创建索赔标识),添加GivenNameSurname索赔类型:

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    // Add the users primary identity details to the set of claims.
    var your_profile = GetFromYourProfileTable();
    identity.AddClaim(new Claim(ClaimTypes.GivenName, your_profile == null ? string.Empty : your_profile.FirstName));
    identity.AddClaim(new Claim(ClaimTypes.Surname, your_profile == null ? string.Empty : your_profile.LastName));
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

然后使用IIdentity的扩展方法从索赔标识中提取信息:

public static ProfileName GetIdentityName(this IIdentity identity)
{
    if (identity == null)
        return null;
    var first = (identity as ClaimsIdentity).FirstOrNull(ClaimTypes.GivenName),
    var last = (identity as ClaimsIdentity).FirstOrNull(ClaimTypes.Surname)
    return string.Format("{0} {1}", first, last).Trim();
}
internal static string FirstOrNull(this ClaimsIdentity identity, string claimType)
{
    var val = identity.FindFirst(claimType);
    return val == null ? null : val.Value;
}

然后,在你的应用程序中(在你的控制器或视图中),你可以这样做:

var name = User.Identity.GetIdentityName();

可以在HttpContext.Current.Items中设置value。这是一个字典,它的生存期是单次请求。

你可以这样使用:

public static string CurrentScreenName
{
    get
    {
        string screenName = (string)HttpContext.Current.Items["CurrentScreenName"];
        if (string.NullOrEmpty(screenName))
        {
             screenName = ResolveScreenName();
             HttpContext.Current.Items["CurrentScreenName"] = screenName;
        }
        return screenName;
    }
}

它将只对单个请求执行一次ResolveScreenName()。

也可以使用扩展方法从IIdentity

访问屏幕名称
public static class Extensions
{
    public static string GetScreenName(this IIdentity identity)
    {
        return CurrentScreenName;
    }
}

然后像这样使用:

string screenName = HttpContext.Current.User.Identity.GetScreenName();

绝对!您需要创建从IPrincipal实现的自己的类型,并自己接管安全性。您可以在OWIN步骤中验证用户,手动设置context.Request.User

我找到了一个实现:

var profile = db.UserProfile.Where(u => u.UserId == user.Id).FirstOrDefault();
ProfileBase httpProfile = ProfileBase.Create(user.UserName);
httpProfile.SetPropertyValue("FullName", profile.FullName);
httpProfile.SetPropertyValue("FirstName", profile.FirstName);
httpProfile.SetPropertyValue("LastName", profile.LastName);

然后得到后来…

ProfileBase userProfile = ProfileBase.Create(HttpContext.User.Identity.Name);
var fullName = userProfile.GetPropertyValue("FullName"));