很遗憾,关于在IdentityServer4中实现自定义AuthorizeInteractionResponseGenerator
的文档非常缺乏。
我正在尝试实现自己的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"
};
}
}
它继承自IdentityServer4中基础的AuthorizeInteractionResponseGenerator,以便标准的登录和同意页面可以显示。这发生了,然后用户被正确地重定向到/Organization
URL来选择组织(租户)。但是接下来呢?由于缺乏文档和示例,我真的很难弄清以下两个问题:
1)选定租户后,我现在如何指示我的自定义AccountChooserResponseGenerator我的交互已经完成,并且现在可以将用户重定向回客户端?
编辑:
答案1:要表示交互已完成,您必须返回一个空的新InteractionResponse()。在我的情况下,检查TenantId声明是否存在就足够了,如下所示:
if (!request.Subject.HasClaim(c=> c.Type == "TenantId" && c.Value != "0"))
return new InteractionResponse
{
RedirectUrl = "/Organization"
};
return new InteractionResponse();
2) 我如何获得关于所选租户的信息,以便添加到IdentityServer4传回客户端的身份令牌中?
编辑:问题2的答案:在选取租户后执行的控制器动作方法中,我调用了:
await HttpContext.SignInAsync(User.Claims.Single(r=> r.Type == "sub").Value,
new System.Security.Claims.Claim("TenantId", tenant.Id.ToString()));
return Redirect(ReturnUrl);
...这是一个由IdentityServer4提供的扩展,用于HttpContext。
Login.cshtml.cs
中创建一个名为public async Task<IActionResult> OnGetFinishLogin(string returnUrl = null)
的方法,它将检查这些额外条件并适当地重定向,如果不需要处理,则重定向回客户端应用程序。 - Richard Barracloughstring tenantSubdomain = (await _interaction.GetAuthorizationContextAsync(returnUrl))?.Parameters["tenant"]
. - Richard Barraclough