你能扩展 HttpContext.Current.User.Identity 属性吗
Can you extend HttpContext.Current.User.Identity properties
有没有办法覆盖 HttpContext.Current.User.Identity
以添加另一个 属性(屏幕名称)?
我的应用程序使用 Identity
并且我将唯一标识保留为电子邮件。我将名字/姓氏等用户数据存储在单独的 "Profile"
table 中。有没有办法将此信息存储在 HttpContext.Current
内的某处?
不一定要在User
以内。我进行了搜索,发现有一个 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;
}
}
它只会对单个请求执行一次 ResolveScreenName()。
您还可以制作扩展方法以从 IIdentity 访问屏幕名称
public static class Extensions
{
public static string GetScreenName(this IIdentity identity)
{
return CurrentScreenName;
}
}
然后像这样使用它:
string screenName = HttpContext.Current.User.Identity.GetScreenName();
如果您使用的是 Asp.Net 身份,那么使用声明就很容易做到这一点。
在您的 SignInAsync
方法中(或者,无论您在何处创建声明标识),添加 GivenName
和 Surname
声明类型:
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.User.Identity
以添加另一个 属性(屏幕名称)?
我的应用程序使用 Identity
并且我将唯一标识保留为电子邮件。我将名字/姓氏等用户数据存储在单独的 "Profile"
table 中。有没有办法将此信息存储在 HttpContext.Current
内的某处?
不一定要在User
以内。我进行了搜索,发现有一个 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;
}
}
它只会对单个请求执行一次 ResolveScreenName()。
您还可以制作扩展方法以从 IIdentity 访问屏幕名称
public static class Extensions
{
public static string GetScreenName(this IIdentity identity)
{
return CurrentScreenName;
}
}
然后像这样使用它:
string screenName = HttpContext.Current.User.Identity.GetScreenName();
如果您使用的是 Asp.Net 身份,那么使用声明就很容易做到这一点。
在您的 SignInAsync
方法中(或者,无论您在何处创建声明标识),添加 GivenName
和 Surname
声明类型:
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();