Asp.net core 返回401,而不是重定向identityserver
我使用ASP.NET Core作为api,无法找到将标识配置为返回401而不是重定向到登录页面的方法。我使用IdentityServer身份验证中间件 在Startup.cs中,我提供以下选项:Asp.net core 返回401,而不是重定向identityserver,asp.net-core,asp.net-identity,identityserver4,Asp.net Core,Asp.net Identity,Identityserver4,我使用ASP.NET Core作为api,无法找到将标识配置为返回401而不是重定向到登录页面的方法。我使用IdentityServer身份验证中间件 在Startup.cs中,我提供以下选项: var options = new IdentityServerAuthenticationOptions { Authority = Configuration.GetConnectionString("Authority"),
var options = new IdentityServerAuthenticationOptions
{
Authority = Configuration.GetConnectionString("Authority"),
ScopeName = "scopeName",
RequireHttpsMetadata = false,
};
app.UseIdentityServerAuthentication(options);
我找到了解决办法,
在请求中,您可以添加一个值为XMLHttpRequest的X-request-header。
这将告诉identity不要重定向到登录页面。要在未经授权时覆盖重定向到identity server的默认行为,我们必须创建一个自定义授权类,实现
System.Web.MVC.AuthorizeAttribute
。然后,在用户未经授权(但通过identity server登录)的情况下,我们向用户显示了一个未经授权的页面
protected override void HandleUnauthorizedRequest(AuthorizationContext
filterContext)
{
if (filterContext == null) {
throw new ArgumentNullException("filterContext");
}
//Intercept results where person is authenticated but still doesn't have
permissions
if (filterContext.RequestContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.Result = new RedirectResult(ConfigSettings.KPToolsURL +
"/Error/HttpError401");
return;
}
base.HandleUnauthorizedRequest(filterContext);
}
我写了一篇关于它的博客文章,其中我详细介绍了它。您是否尝试在IdentityServerAuthenticationOptions上设置AutomaticChallenge=false?我尝试了,但没有解决问题。您是否尝试添加了一个
AccessDeniedPath=new PathString(“/Account/Forbidden/”)
您的身份验证?是的,我试过了,但没有解决问题。