C# 您可以扩展HttpContext.Current.User.Identity属性吗

C# 您可以扩展HttpContext.Current.User.Identity属性吗,c#,asp.net-mvc-5,asp.net-identity,C#,Asp.net Mvc 5,Asp.net Identity,是否有方法重写HttpContext.Current.User.Identity以添加另一个属性(屏幕名称) 我的应用程序使用标识,并且我将唯一标识保留为电子邮件。我将名/姓等用户数据存储在单独的“Profile”表中。是否有办法将此信息存储在HttpContext.Current中的某个位置 它不一定需要在用户中。我搜索了一下,发现有一个HttpContext.Current.ProfileBase。但我不知道如何使用它——而且我真的不想要base附带的所有多余的东西。我找到了一个实现: va

是否有方法重写HttpContext.Current.User.Identity以添加另一个属性(屏幕名称)

我的应用程序使用
标识
,并且我将唯一标识保留为电子邮件。我将名/姓等用户数据存储在单独的
“Profile”
表中。是否有办法将此信息存储在
HttpContext.Current
中的某个位置


它不一定需要在
用户
中。我搜索了一下,发现有一个
HttpContext.Current.ProfileBase
。但我不知道如何使用它——而且我真的不想要base附带的所有多余的东西。

我找到了一个实现:

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"));

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

您可以在HttpContext.Current.Items中输入值。 它的生命周期是单个请求

您可以这样使用它:

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;
    }
}
string screenName = HttpContext.Current.User.Identity.GetScreenName();
对于单个请求,它将只执行一次ResolveScreenName()

您还可以使用扩展方法从IIdentity访问屏幕名称

public static class Extensions
{
    public static string GetScreenName(this IIdentity identity)
    {
        return CurrentScreenName;
    }
}
然后像这样使用它:

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;
    }
}
string screenName = HttpContext.Current.User.Identity.GetScreenName();

如果您使用的是Asp.Net标识,那么这很容易处理声明

在您的
SignInAsync
方法中(或者,无论您在何处创建索赔标识),添加
GivenName
姓氏
索赔类型:

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();

据我所知,您有不同的表和其他用户数据。您需要获取当前用户的数据。我说得对吗?您正在使用实体框架吗?而您更喜欢将这些数据存储到另一个表中的方式呢?
User
实际上只是一个
i原则
Identity
就是
IIdentity
。您可以使用自己的
Identity
实现轻松创建自己的
User
实现。他们只需要遵守接口。因为您使用的是Asp.Net标识,所以处理的方法是声明。请参阅下面发布的我的答案。谢谢,我今晚正在测试答案,并将接受一个答案,然后可以覆盖
Name
属性吗?@ErwinRooijakkers什么的
Name
属性?您能提供一些更详细的信息吗?
HttpContext.Current.User.Identity.Name
。另见。