asp.net - 如何为现有ClaimsIdentity指定目的地?

标签 asp.net asp.net-identity openid-connect aspnet-contrib

我正在使用下面的代码在OpenIdConnectServerProvider.AuthorizationProvider中创建ClaimIdentity。但是identity.Name不会被标准化。如何让OpenIdConnectServer序列化名称?谢谢。

上一个问题在这里How to create a ClaimIdentity in asp.net 5

var user = await userManager.FindByNameAsync(context.UserName);
var factory = context.HttpContext.RequestServices.GetRequiredService<IUserClaimsPrincipalFactory<ApplicationUser>>();
var identity = await factory.CreateAsync(user);                
context.Validated(new ClaimsPrincipal(identity));       

最佳答案

为了避免泄露 secret 数据,AspNet.Security.OpenIdConnect.Server拒绝序列化未明确指定目的地的声明。

要序列化名称(或任何其他声明),可以使用.SetDestinations扩展名:

var principal = await factory.CreateAsync(user);

var name = principal.FindFirst(ClaimTypes.Name);
if (name != null) {
    // Use "id_token" to serialize the claim in the identity token or "access_token"
    // to serialize it in the access token. You can also specify both destinations.
    name.SetDestinations(OpenIdConnectConstants.Destinations.AccessToken,
                         OpenIdConnectConstants.Destinations.IdentityToken);
}

context.Validate(principal);

添加声明时,还可以使用带有AddClaim参数的destinations扩展名:
identity.AddClaim(ClaimTypes.Name, "Pinpoint",
     OpenIdConnectConstants.Destinations.AccessToken,
     OpenIdConnectConstants.Destinations.IdentityToken);

关于asp.net - 如何为现有ClaimsIdentity指定目的地?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33797838/

相关文章:

c# - .net 强类型 View 模型未设置为对象实例

c# - 托管时 ASP.Net Com-InterOp Excel 生成问题

c# - 为什么 mvcaction4 代码片段没有反应?

c# - 带有 IdentityDbContext 的 EF6 多对多无效列名

python - Python 中的 OpenID Connect 提供程序

asp.net - 如何在移动浏览器中有效地使用动态 CSS?

.net - 实现 UserManager 以使用自定义类和存储过程

asp.net-mvc - 删除外部帐户后,ASP.net Identity 停止分发外部 Cookie

oauth-2.0 - 向客户端(例如浏览器)共享 AccessToken 是一个安全问题吗?

asp.net-mvc - AllowAnonymous 不适用于 azure 广告身份验证