- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
让我全神贯注于新的身份框架,并试图弄清楚如何最好地处理自定义用户属性。我已经尝试扩展 IdentityUser,它用于存储信息,但到目前为止需要额外的数据库调用才能恢复属性。我正在考虑改用声明来存储/检索此信息。
首先,我要存储/检索的特定 prop 不是单个用户所独有的(多对一)。考虑在自定义组结构中将用户分组在一起。我想存储 GroupId 以便在其他相关实体中使用。
我能够存储 GroupId(目前使用 ClaimTypes.NameIdentifier,我认为它不是该类型的正确用法,但是...)。但是,当我去检索该值时,在声明集合中找不到声明类型。它在数据库中,所以我知道它在那里。我错过了一些东西。
FWIW:因为它是 WebAPI,所以我没有使用传统的登录方式。我正在使用 token 授权。
当我创建用户时,我有类似的东西:
public async Task<IdentityResult> CreateUserAsync(string email, string password, string groupId)
{
var userId = ObjectId.GenerateNewId(DateTime.UtcNow).ToString(); // yes, it's a NoSQL store
var user = new ApplicationUser
{
Id = userId,
UserName = email
};
var claim = new IdentityUserClaim { ClaimType = ClaimTypes.NameIdentifier, ClaimValue = groupId, UserId = userId, Id = ObjectId.GenerateNewId(DateTime.UtcNow).ToString() };
user.Claims.Add(claim);
var result = await _UserManager.CreateAsync(user, password);
return result;
}
这会创建一个看起来合适的数据库条目。
当我检索值时,出现空引用错误。这是通过扩展方法编写的代码:
public static string GetGroupId(this IIdentity identity)
{
var claimsIdentity = identity as ClaimsIdentity;
return claimsIdentity == null ? "" : claimsIdentity.FindFirst(ClaimTypes.NameIdentifier).Value;
}
尝试获取值时发生错误,因为 FindFirst 返回空值。
如有任何提示或更好/最佳实践,我们将不胜感激!老实说,我更愿意将它存储在 ApplicationUser : IdentityUser
对象上,但我找不到一种简单的方法来检索 User.Identity 在我的 api Controller 上下文中,而无需额外调用数据库。
最佳答案
您关于将额外数据存储为声明的直觉是正确的,但实现起来有点问题。
我建议为您的域信息创建您自己的声明类型。不要重用框架提供的声明类型。原因是 ClaimTypes.NameIdentifier
代表 User.Id。该框架本身为所有用户添加了标准声明列表:
ClaimTypes.NameIdentifier
ClaimTypes.ProviderName
(对此不是 100% 确定);通常值为“ASP.NET Identity”ClaimTypes.Role
因此,在您的情况下,您尝试用 User.Id
的值覆盖声明,这非常重要,我认为 -)
现在,让我们尝试解决您的编码问题。创建用户时,在创建用户对象后添加声明:
public async Task<IdentityResult> CreateUserAsync(string email, string password, string groupId)
{
var user = new ApplicationUser
{
Id = userId,
UserName = email
};
var userCreateResult = await _UserManager.CreateAsync(user, password);
if(!userCreateResult.IsSuccess)
{
// user creation have failed - need to stop the transaction
return userCreateResult;
}
// better to have a class with constants representing your claim types
var groupIdClaim = new Claim("MyApplication:GroupClaim", ObjectId.GenerateNewId(DateTime.UtcNow).ToString());
// this will save the claim into the database. Next time user logs in, it will be added to Principal.Identity
var claimAddingResult = await _UserManager.AddClaimAsync(userId, groupIdClaim);
return claimAddingResult;
}
至于扩展方法,我通常使用 IPrincipal
或 ClaimsPrincipal
。但是 IIdentity
也是可行的。不要忘记,您可以通过调用 ClaimsPrincipal.Current
在任何地方访问 ClaimsPrincipal
。
这就是我通常使用扩展方法的方式:
public static string GetGroupId(this ClaimsPrincipal principal)
{
var groupIdClaim = principal.Claims.FirstOrDefault(c => c.Type == "MyApplication:GroupClaim");
if (personIdClaim != null)
{
return groupIdClaim.Value;
}
return String.Empty;
}
因此在您的方法中,您会像这样为当前登录的用户检索分配的 groupId
:
var groupId = ClaimsPrincipal.Current.GetGroupId();
希望这能澄清您的困惑!
关于c# - 使用 OWIN Identity v2 Claims 跟踪 WebAPI2 应用程序中的自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25328717/
如果达到我的自定义政策中的特定步骤/条件,我将尝试从 claim 包中删除所有 claim ,但我找不到任何相关信息。然后我尝试对字符串使用 NullClaim Transformation,但我找不
我正在实现一个 oAuth 服务器并且需要存储刷新 token ,为此我(目前)选择将 token 序列化为 JSON。 虽然我可以看到 JSON 包含重新水化所需的一切,但当我使用 token.Fr
我正在创建一个简单的 WebApi,它允许用户连接到 Facebook。当我从 facebook 取回 accessToken 时,我调用 RegisterExternal 来创建一个 Asp.Net
我似乎无法序列化 Claim - 我做错了什么?使用此方法的其他对象序列化工作正常。 代码 var client = new MongoClient("mongodb://localhost:2701
我正在尝试在 ASP.NET Core 中使用基于 Windows 身份验证的声明授权。我有一个设置用户声明的声明转换器。我还通过 DbContext 将 IPrincipal 注入(inject)到
我直接使用 AspNetUserClaims 表,因为它主要属于我的 ASP.NET 应用程序,但也被 Windows 应用程序使用。 ASP.NET 应用程序使用 Google 进行外部登录,而 W
我从 ASP.NET Identity 的声明授权开始,如果我的应用程序中需要“角色”概念,我想阐明处理它们的方式。 注意:我对这个真的很陌生,所以所有的概念都在我脑海中飞舞,请多多关照,对于任何概念
我已经安装了 WSO2 IS 5.2.0,但我无法检索自定义创建的声明。 我为方言添加了新声明 http://wso2.org/claims我还添加了将相同属性映射到方言的新声明 http://wso
我们正在尝试使用组织 AD 作为 IDP,使用 SAML 和 Azure B2C 作为服务提供商 (SP) 设置 SSO。Org AD 被许多其他 SP 供应商用作 IDP(SAML),并且工作得很好
我正在开发一个 MVC4 应用程序,该应用程序使用 ADFS 对公司域用户进行身份验证,我已经编写了代码,但收到如下错误,我是否没有收到 claim ? System.InvalidCastExcep
我收到针对 Azure AD 进行身份验证的用户的组超额 claim 。如下所示: {"src1":{"endpoint":"https://graph.windows.net/TENANTID/us
我收到针对 Azure AD 进行身份验证的用户的组超额 claim 。如下所示: {"src1":{"endpoint":"https://graph.windows.net/TENANTID/us
是否可以将 SAML 声明发送到 ADFS,然后让 ADFS 使用来自传入声明的值来生成自己的声明? 基本上,我们需要发送 a)关于用户的信息(相当简单),以及 b)关于目标的信息(手头的问题)。目标
我目前正在开发 API。 token 从 IdentityServer4 返回。 我正在尝试从 token 声明中取回子 ID,即当前授权用户的 ID。我可以在此处的声明中看到它。 { "nbf"
我目前正在使用 asp.net core 设计一个系统,我想实现基于声明的授权,但有一个特定的部分让我感到困惑。 当提出声明时,声明将包括类型和值以及可选的发行者。在处理程序中,可以在确认访问之前检查
我需要从 JWT 中提取声明。 看来这应该是理所当然的。 它已签名,从我得到的标题中: { "alg": "RS256", "typ": "JWT" } 智威汤逊: eyJhbGciOiJSU
我是一个长期的 .NET Framework/WebForms 开发人员,并且正在为一个新项目转向 Core (2.1)。我已经建立了基础部分,一个 VS 解决方案包含一个 DAL 项目和一个 Web
我正在尝试根据特定声明的值过滤用户。 我使用 ASP .Net Core 3 和 RavenDB。 filteredData = data.Search(d => d.Email, model.sea
我将 WSO2 身份服务器用于单点登录实现。 在我的演示应用程序中,我试图从我自己的 JDBC 数据库中获取经过身份验证的用户的自定义声明属性。 我关注了这个 blog普什帕兰卡。 这适用于 Iden
我想通过Identity内的Claim访问我的电子邮件地址 我尝试访问为: var email = User.Identity.GetClaimsByType("emailaddress").ToSt
我是一名优秀的程序员,十分优秀!