你能扩展 HttpContext.Current.User.Identity 属性吗 [英] Can you extend HttpContext.Current.User.Identity properties

查看:9
本文介绍了你能扩展 HttpContext.Current.User.Identity 属性吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to override HttpContext.Current.User.Identity to add another property (screen name)?

我的应用程序使用 Identity 并且我将唯一身份作为电子邮件保留.我将用户数据(例如名字/姓氏)存储在单独的 "Profile" 表中.有没有办法将这些信息存储在 HttpContext.Current 中的某处?

My application uses Identity and I've left the unique identity as email. I store user data such as first / last name in a separate "Profile" table. Is there a way to store this information somewhere within HttpContext.Current?

它不一定需要在 User 内.我进行了搜索并注意到有一个 HttpContext.Current.ProfileBase.虽然不知道如何使用它 - 我真的不想要 base 附带的所有多余的东西.

It doesn't necessarily need to be within User. I have had a search and noticed there's a HttpContext.Current.ProfileBase. Not sure how to use it though - and I really don't want all the excess stuff that base comes with.

推荐答案

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

If you are using Asp.Net Identity, then this is very easy to do with claims.

在您的 SignInAsync 方法中(或者,无论您在何处创建声明标识),添加 GivenNameSurname 声明类型:

In your SignInAsync method (or, wherever you are creating the claims identity), add the GivenName and Surname claim types:

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 的扩展方法从声明标识中提取信息:

You then use an extension method to IIdentity to pull the information out of the claims identity:

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;
}

然后,在您的应用程序中(在您的控制器或视图中),您可以执行以下操作:

Then, in your application (in your controller or view), you can just do:

var name = User.Identity.GetIdentityName();

这篇关于你能扩展 HttpContext.Current.User.Identity 属性吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆