我试图理解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
在做什么?我想它可能会从Query字符串中提取一些授权代码并授权。正确吗?
谢谢!我想了解一下IdentityServer4快速启动中的这个条件。
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("~/");
}
}
授权上下文 叙述于 文件:
IdentityServer将传递一个returnUrl参数(可在用户交互选项中配置)到同意页面,其中包含授权请求的参数。这些参数为同意页提供了上下文,并且可以在交互服务的帮助下被读取。GetAuthorizationContextAsync API将返回一个AuthorizationRequest的实例。