我有一个登录页面,当用户单击“取消”按钮时我需要将他重定向到客户端应用程序上访问被拒绝的页面。
内部登录操作:
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);
}
}
在客户端(MVC)我配置了以下事件:
options.Events = new OpenIdConnectEvents
{
OnRemoteFailure = context =>
{
// here it's returned as 200 ok in case I denied
// consent should'nt be 401 access denined??
var statusCode=context.Response.StatusCode;
context.Response.Redirect("/");
context.HandleResponse();
return Task.FromResult(0);
}
};
但我的问题是:我怎么知道 IdentityServer4 失败是因为用户单击了取消按钮 (access_denied) 还是其他问题导致了失败?
最佳答案
在 IdentityServer 端:
基本表单有 2 个按钮:login
和 cancel
。如果 login
没有被按下;这是一个取消
。
否则它是一个验证错误,你可以显示它。在 cancel
上,您应该重定向回有意义的页面。
在 MVC 方面:
您可以使用额外的参数进行重定向。这些可以被获取并用于显示错误。请记住,许多错误处理(例如无效的用户名/密码)都在 IdentityServer 端进行。
关于c# - 使用IdentityServer4时如何在客户端检查access_denied?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53408511/