c# - 使用 IdentityServer4 时如何在客户端检查 access_denied?
问题描述
我有一个登录页面,当用户单击取消按钮时我需要将他重定向到访问客户端应用程序上的拒绝页面。
内部登录操作:
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
。
否则它是一个验证错误,你可以显示它。在cancel
你应该重定向回一个有意义的页面。
在 MVC 方面:
您可以使用额外的参数进行重定向。这些可以被获取并用于显示错误。请记住,许多错误处理,例如无效的用户名/密码都留在 IdentityServer 端。
推荐阅读
- php - PHP:一旦用户关闭浏览器,我如何在我的数据库上执行与特定会话相关的操作?
- wordpress - Yoast SEO - 禁用自定义帖子类型的 OpenGraph 标签
- oracle - 使用 MATCH_RECOGNIZE 时 FINAL SUM 的 NULLIF 翻倍
- python - 如何使用`httpretty`模拟请求
- python - 如何在 Angular 9 中集成 python 脚本?
- reactjs - 当元素出现在 50 张卡片上时,如何使用 .map() 函数在点击屏幕上显示的元素之一下方放置一个框
- c# - 如何使用正确的 MVVM 在不同的内容页面 Xamarin 中传递数据
- git - 德比安。当我使用 `git restore` 命令时,它说,这个命令不存在
- r - 如何根据字符串的第一个和第三个字符过滤一列字符串?
- java - 将 ArrayList 中的两组值与重复配对的逻辑