2023-02-15 304
我正在尝试构建混合流,并在带有IdentityServer4的返回访问令牌上索赔.我正在使用 QuickStart UI Controlles.
在我的AccountController成功身份验证后,我有以下代码签名他:
await HttpContext.SignInAsync("anon@nymous.com", "anon@nymous.com", null, new Claim("MyName", "Ophir"));
在导致此流程的MVC网站上,我想”保护”的页面上有以下代码:
[Authorize]
public IActionResult RestrictedMvcResource()
{
var token = _httpContext.HttpContext.GetTokenAsync("access_token").Result;
var identity = User.Identity;
return View();
}
成功登录后,调试器打入此代码罚款,我将获得访问令牌.
问题是,如果我解码我的访问令牌(我正在使用 https://jwt.io/a/a >)我看到了名称和主题,但是我看不到MyName声称我已经定义了.
(我的系统中有另一个流量client_credentials确实返回令牌上的索赔 – 但它使用了不同的代码流).
如何返回混合流的令牌上的索赔?
编辑:
解决这个问题是两件事的组合:
public class ProfileService : IProfileService
{
public Task GetProfileDataAsync(ProfileDataRequestContext context)
{
context.AddRequestedClaims(context.Subject.Claims);
foreach (Claim claim in context.Subject.Claims)
{
if (context.IssuedClaims.Contains(claim))
continue;
context.IssuedClaims.Add(claim);
}
return Task.FromResult(0);
}
public Task IsActiveAsync(IsActiveContext context)
{
context.IsActive = true;
return Task.FromResult(0);
}
}
这将添加任何尚未在令牌上的主张.
您可以实现自定义IProfileService如果要将自定义索赔添加到令牌.
您可以在身份服务器4 docs 中找到更多信息.
简单的自定义个人资料服务的一个示例是:
public class CustomProfileService : IProfileService
{
public Task GetProfileDataAsync(ProfileDataRequestContext context)
{
context.AddRequestedClaims(context.Subject.Claims);
context.IssuedClaims.Add(new Claim("MyName", "Ophir"));
return Task.FromResult(0);
}
public Task IsActiveAsync(IsActiveContext context)
{
context.IsActive = true;
return Task.FromResult(0);
}
}
一旦您将其注册到DI:
services.AddTransient<IProfileService, CustomProfileService>();
每当请求access_token或id_token时,它都会被调用.如果您只想要某些类型的令牌索赔,则需要根据Ruard的评论来检查context.Caller.
编辑:
Also alternatively, you can add the claims directly to the user configuration as per example in one of the Identity Server 4 QuickStarts :
new TestUser
{
SubjectId = "1",
Username = "alice",
Password = "password",
Claims = new []
{
new Claim("MyName", "Ophir")
}
},
如果您最终不实现自定义IProfileService并继续使用DefaultProfileService,那么您还需要在配置中添加自定义IdentityResource:
return new List<IdentityResource>
{
//..Your other configured identity resources
new IdentityResource(
name: "custom.name",
displayName: "Custom Name",
claimTypes: new[] { "MyName" });
};
任何想要在令牌中添加此主张的客户都需要custom.name范围的请求.
aspnet.security.openidconnect.server不会序列化没有目的地设置的主张.我在使用OpenIddict时遇到了这个.
尝试以下操作:
var claim = new Claim("MyName", "Ophir");
claim.SetDestinations(OpenIdConnectConstants.Destinations.AccessToken);
await HttpContext.SignInAsync("anon@nymous.com", "anon@nymous.com", null, claim);
您可能需要添加这些名称空间:
using AspNet.Security.OpenIdConnect.Extensions;
using AspNet.Security.OpenIdConnect.Primitives;
有两个步骤,我为我们的身份服务器添加了索赔.
通过像其他答案之一一样,通过自定义个人资料服务,您可以为用户定义自己的主张.
然后可以通过UserInfo端点请求这些主张.
或创建一个api(资源),例如,如果您要求该API作为范围,则默认为访问令牌添加名称声明.
.
以上所述是小编给大家介绍的身份服务器4声明不是混合流上的令牌的一部分,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/33753.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态