简体   繁体   English

IdentityServer4 快速入门登录问题

[英]IdentityServer4 QuickStart Login question

I am trying to understand this condition in IdentityServer4 quickstart:我试图在 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("~/");
            }
        }

As far as I understand, if login form is not submitted by pressing login button ( <button type=submit value=login> ) but by another post request (?) what exactly is going to happen?据我了解,如果登录表单不是通过按下登录按钮( <button type=submit value=login> )而是通过另一个发布请求(?)提交的,究竟会发生什么?

What is GetAuthorizationContextAsync doing? GetAuthorizationContextAsync在做什么? I think it may extract some Authorization code from Query string and Authorize.我认为它可能会从查询字符串和授权中提取一些授权代码。 Correct?正确的?

Thanks!谢谢!

The QuickStart example contains also comments in the code that explain what the method is doing: QuickStart 示例还包含代码中的注释,用于解释该方法的作用:

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("~/");
    }
} 

Authorization context is described in documentation :授权上下文文档中描述:

IdentityServer will pass a returnUrl parameter (configurable on the user interaction options) to the consent page which contains the parameters of the authorization request. IdentityServer 会将 returnUrl 参数(可在用户交互选项上配置)传递给包含授权请求参数的同意页面。 These parameters provide the context for the consent page, and can be read with help from the interaction service.这些参数为同意页面提供上下文,并且可以在交互服务的帮助下读取。 The GetAuthorizationContextAsync API will return an instance of AuthorizationRequest. GetAuthorizationContextAsync API 将返回 AuthorizationRequest 的实例。

This trick with the named button value is a commonly used trick to have multiple buttons to submit the same form.这个具有命名按钮值的技巧是一个常用的技巧,可以让多个按钮提交同一个表单。 Either clicking cancel or login button will trigger submission of the login form, but the handling of the submission will be handled differently.单击取消或登录按钮将触发登录表单的提交,但提交的处理将有所不同。

For your second question: this related to the configured clients within the IdentityServer configuration.对于您的第二个问题:这与 IdentityServer 配置中配置的客户端有关。 Based on the return URL, the correct client is retrieved from the IdentityServer configuration.根据返回 URL,从 IdentityServer 配置中检索到正确的客户端。 While getting this context, there is also validation triggered to see if the return URL is a known configured return URL.在获取此上下文时,还会触发验证以查看返回 URL 是否是已知的已配置返回 URL。

This is later used to determine the correct ClientId, and wether PKCE validation is required or not for the configured client, to properly handle the login request (either cancelled, or not).这稍后用于确定正确的 ClientId,以及配置的客户端是否需要 PKCE 验证,以正确处理登录请求(取消或不取消)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM