Sha*_*Wet 6 c# identityserver4
可悲的AuthorizeInteractionResponseGenerator是,非常缺少有关IdentityServer4中自定义实现的文档。
我正在尝试实现自己的方法,AuthorizeInteractionResponseGenerator因为我需要进一步的用户交互步骤(在身份验证之后)。我的情况是,一个身份(电子邮件)可以与多个租户关联。因此,登录后,我需要向用户显示相关租户的列表,以便他们可以选择一个。
我已经评估了源代码,并提出了以下定制AuthorizeInteractionResponseGenerator:
public class AccountChooserResponseGenerator : AuthorizeInteractionResponseGenerator
{
public AccountChooserResponseGenerator(ISystemClock clock,
ILogger<AuthorizeInteractionResponseGenerator> logger,
IConsentService consent, IProfileService profile)
: base(clock, logger, consent, profile)
{
}
public override async Task<InteractionResponse> ProcessInteractionAsync(ValidatedAuthorizeRequest request, ConsentResponse consent = null)
{
var response = await base.ProcessInteractionAsync(request, consent);
if (response.IsConsent || response.IsLogin || response.IsError)
return response;
return new InteractionResponse
{
RedirectUrl = "/Organization"
};
}
}
Run Code Online (Sandbox Code Playgroud)
它继承自IdentityServer4内置的基本AuthorizeInteractionResponseGenerator,因此可以显示标准的“登录”和“同意”页面。发生这种情况,然后将用户正确重定向到该/OrganizationURL以选择组织(承租人)。
但是那又怎样呢?由于缺乏文档和示例,我真的很难找出以下两个问题:
1)现在,如何在选择租户后向我的自定义AccountChooserResponseGenerator指示我的交互已完成,并且现在可以将用户重定向回客户端?
编辑:
对1的答案:为了表明交互已完成,您必须返回一个空的新InteractionResponse()。就我而言,检查TenantId声明是否存在就足够了,如下所示:
if (!request.Subject.HasClaim(c=> c.Type == "TenantId" && c.Value != "0"))
return new InteractionResponse
{
RedirectUrl = "/Organization"
};
return new InteractionResponse();
Run Code Online (Sandbox Code Playgroud)
2)以及如何获取有关要添加到IdentityServer4传递回客户端的身份令牌中的选定租户的信息?
编辑:答案2:在选择租户后执行的Controller Action方法中,我调用了:
await HttpContext.SignInAsync(User.Claims.Single(r=> r.Type == "sub").Value,
new System.Security.Claims.Claim("TenantId", tenant.Id.ToString()));
return Redirect(ReturnUrl);
Run Code Online (Sandbox Code Playgroud)
...这是IdentityServer4提供的 HttpContext 扩展。
小智 1
要在 Identity Server 4 中实现自定义交互响应生成器并将有关所选租户的信息添加到身份令牌,您可以从基础继承AuthorizeInteractionResponseGenerator并重写该ProcessInteractionAsync方法。您还可以使用IProfileServiceIdentity Server 4 提供的接口在颁发令牌之前添加租户信息作为用户身份的声明。
实现自定义交互响应生成器的步骤:
从基类继承AuthorizeInteractionResponseGenerator并重写ProcessInteractionAsync方法:
// Example implementation of ProcessInteractionAsync
public override async Task<InteractionResponse> ProcessInteractionAsync(ValidatedAuthorizeRequest request, ConsentResponse consent = null)
{
// Your custom logic here
// Indicate that the interaction is complete
return new InteractionResponse();
}
Run Code Online (Sandbox Code Playgroud)
将有关所选租户的信息添加到身份令牌:
// Example implementation of IProfileService
public class CustomProfileService : IProfileService
{
private readonly UserManager<ApplicationUser> _userManager;
public CustomProfileService(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
{
var user = await _userManager.GetUserAsync(context.Subject);
if (user != null)
{
var tenantIdClaim = user.Claims.FirstOrDefault(c => c.Type == "TenantId");
if (tenantIdClaim != null)
{
context.IssuedClaims.Add(new Claim("tenant_id", tenantIdClaim.Value));
}
}
}
public Task IsActiveAsync(IsActiveContext context)
{
return Task.CompletedTask;
}
}
Run Code Online (Sandbox Code Playgroud)
CustomProfileService在您的文件中向 Identity Server 4注册Startup.cs:
services.AddTransient<IProfileService, CustomProfileService>();
Run Code Online (Sandbox Code Playgroud)
尖端:
IConsentService和IProfileService注入您的生成器来访问用户配置文件和同意数据。| 归档时间: |
|
| 查看次数: |
747 次 |
| 最近记录: |