C# PKCE代码流中重定向页面的必要性(IdentityServer4)

C# PKCE代码流中重定向页面的必要性(IdentityServer4),c#,asp.net,authorization,identityserver4,pkce,C#,Asp.net,Authorization,Identityserver4,Pkce,使用带有IdentityServer4的PKCE代码流处理项目。我在IS4示例中发现了这种代码的平静: [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Login(LoginInputModel model, string button) { // check if we are in the context of an authorization request var c

使用带有IdentityServer4的PKCE代码流处理项目。我在IS4示例中发现了这种代码的平静:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginInputModel model, string button)
{
    // check if we are in the context of an authorization request
    var context = await _interaction.GetAuthorizationContextAsync(model.ReturnUrl);

    // the user clicked the "cancel" button
    if (button != "login")
    {
        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
            if (await _clientStore.IsPkceClientAsync(context.ClientId))
            {
                // if the client is PKCE then we assume it's native, so this change in how to
                // return the response is for better UX for the end user.
                return View("Redirect", new RedirectViewModel { RedirectUrl = model.ReturnUrl });
            }

            return Redirect(model.ReturnUrl);
        }
        else
        {
            // since we don't have a valid context, then we just go back to the home page
            return Redirect("~/");
        }
    }
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务登录(LoginInputModel模型,字符串按钮)
{
//检查我们是否在授权请求的上下文中
var context=await\u interaction.GetAuthorizationContextAsync(model.ReturnUrl);
//用户单击了“取消”按钮
如果(按钮!=“登录”)
{
if(上下文!=null)
{
//如果用户取消,则将结果发送回IdentityServer,就像他们
//拒绝同意(即使该客户不需要同意)。
//这将向客户端发回拒绝访问的OIDC错误响应。
wait_interaction.GrantConsentAsync(上下文、同意响应、拒绝);
//我们可以信任model.ReturnUrl,因为GetAuthorizationContextAsync返回了非null
if(wait_clientStore.IsPkceClientAsync(context.ClientId))
{
//如果客户机是PKCE,那么我们假设它是本机的,因此
//返回的响应是为最终用户提供更好的用户体验。
返回视图(“重定向”,新的重定向视图模型{RedirectUrl=model.ReturnUrl});
}
返回重定向(model.ReturnUrl);
}
其他的
{
//因为我们没有有效的上下文,所以我们只需返回主页
返回重定向(“~/”);
}
}

所以问题是我们为什么要显示重定向页面以及它如何改进用户体验?Redirec视图上唯一的内容是消息“您现在正在返回到应用程序”。您能告诉我这样做的原因或任何必要的情况吗?谢谢!

这对具有自定义重定向URI的本机客户端非常有用。如果您不需要,请随时删除代码。

并不是只显示一条消息。请注意refresh meta和signin-redirect.js脚本。