IdentityServer4快速入门登录问题

时间:2020-06-15 13:18:58

标签: c# identityserver4

我试图在IdentityServer4快速入门中了解这种情况:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(LoginInputModel model, string button)
    {
        if (button != "login")
        {
            var context = await _interaction.GetAuthorizationContextAsync(model.ReturnUrl);
            if (context != null)
            {
                await _interaction.GrantConsentAsync(context, ConsentResponse.Denied);
                return Redirect(model.ReturnUrl);
            }
            else
            {
                return Redirect("~/");
            }
        }

据我了解,如果登录表单不是通过按登录按钮(<button type=submit value=login>)提交,而是通过另一个发帖请求(?)提交,究竟会发生什么?

GetAuthorizationContextAsync在做什么?我认为它可能会从查询字符串和授权中提取一些授权代码。正确吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

“快速入门”示例还包含代码中的注释,这些注释解释了该方法的作用:

if (button != "login")
{
    // the user clicked the "cancel" button
    var context = await _interaction.GetAuthorizationContextAsync(model.ReturnUrl);
    if (context != null)
    {
        // if the user cancels, send a result back into IdentityServer as if they 
        // denied the consent (even if this client does not require consent).
        // this will send back an access denied OIDC error response to the client.
        await _interaction.GrantConsentAsync(context, ConsentResponse.Denied);

        // we can trust model.ReturnUrl since GetAuthorizationContextAsync returned non-null
        return Redirect(model.ReturnUrl);
    }
    else
    {
        // since we don't have a valid context, then we just go back to the home page
        return Redirect("~/");
    }
} 

授权上下文documentation中进行了描述:

IdentityServer将传递一个returnUrl参数(可在 用户交互选项)到包含 授权请求的参数。这些参数提供了 同意页面的上下文,并且可以在 互动服务。 GetAuthorizationContextAsync API将返回 一个AuthorizationRequest实例。

答案 1 :(得分:0)

这个具有指定按钮值的技巧是commonly used trick,可以让多个按钮提交相同的表单。单击“取消”或“登录”按钮将触发提交登录表单,但是提交的处理方式将有所不同。

第二个问题:这与IdentityServer配置中配置的客户端有关。根据返回URL,正确的客户端是IdentityServer配置中的retrieved。获取此上下文时,还会触发validation来查看返回URL是否是已知的已配置返回URL。

稍后将其用于确定正确的ClientId,并且配置的客户端是否需要PKCE验证才能正确处理登录请求(已取消或未取消)。