。NET 4.5 MVC网站中使用IdentityServer4时在授权上的无限循环

问题描述 投票:1回答:1

我正在尝试使用IdentityServer4对现有的.NET 4.5 MVC网站进行身份验证/授权。这个应用程式正在使用较旧的IdentityServer版本。代码更改后,登录有效,但是会出现无限循环。应用程序日志说,循环正在调用Identity Server的/authorize。我目前的情况:-我正在使用OpenID connect-我无法更改授权规则。-我无法升级.NET版本,它是旧版/正常运行的网站。有人可以帮我知道为什么会这样吗?

asp.net-mvc-5 authorization identityserver4 openid-connect
1个回答
0
投票

简单地说,IdentityServer4的默认行为是将所有未经授权的用户重定向到IdentityServer,这可能会导致无限循环。您可以通过在MVC应用上手动处理401来解决此问题。如果没有,请尝试添加自定义AuthorizeAttribute,并像这样覆盖HandleUnauthorizedRequest

protected override void HandleUnauthorizedRequest(AuthorizationContext 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);
}

读取模式herehere

© www.soinside.com 2019 - 2024. All rights reserved.