我遇到了login.microsoftonline.com和我的应用程序之间的无限重定向循环。我的项目是在Asp.net 4.8WebForms项目中实现身份验证和授权。我能够使用默认的Owin启动文件添加身份验证,然后在web配置文件中要求身份验证。下面的操作正确地要求用户在能够访问pages/AuthRequired
之前登录
StartupAuth.CS
public partial class Startup
{
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
private static string authority = ConfigurationManager.AppSettings["ida:Authority"];
private static string clientSecret = ConfigurationManager.AppSettings["AppRegistrationSecret-Local"];
public void ConfigureAuth(IAppBuilder app)
{
//for debugging
//IdentityModelEventSource.ShowPII = true;
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
ClientSecret = clientSecret,
RedirectUri = postLogoutRedirectUri,
//This allows multitenant
//https://github.com/Azure-Samples/guidance-identity-management-for-multitenant-apps/blob/master/docs/03-authentication.md
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthenticationFailed = (context) =>
{
return Task.FromResult(0);
}
}
}
);
// This makes any middleware defined above this line run before the Authorization rule is applied in web.config
app.UseStageMarker(PipelineStage.Authenticate);
}
}
Web.Config
<configuration>
...
<system.web>
<authentication mode="None" />
</system.web>
<location path="Pages/AuthRequired">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
</system.webServer>
...
</configuration>
我需要添加授权,这样只有具有管理角色的用户才能访问Pages/AuthRequired
。我是通过更新web配置来做到这一点的:
<configuration>
...
<system.web>
<authentication mode="None" />
</system.web>
<location path="Pages/AuthRequired">
<system.web>
<authorization>
<allow roles="Admin" />
<deny users="*" />
</authorization>
</system.web>
</location>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
</system.webServer>
...
</configuration>
如果用户具有该角色,则向经过身份验证的页面添加授权是正确的,但是如果没有该角色的用户试图访问该页面,则会将其重定向回login.microsoftonline.com,然后在无限循环中返回到应用程序。
我可以看到Owin UseOpenIdConnectAuthentication在未经授权的情况下返回302个响应,这导致了循环。
我怎样才能改变它,而不是将未经授权的(但经过身份验证的)用户重定向到login.microsoftonline.com,而是将用户定向到显示401错误的应用程序页面?
发布于 2022-04-24 13:22:35
请检查下面的工作是否有帮助:
通常情况下,如果启用了forms authentication
,当状态代码为401时,您将被重定向到登录页面。
作为一种解决方法,尝试在应用程序结束请求中将下面的内容添加到global.asax中,如果需要,您可以创建自己的未经授权的页面并重定向到该页面。
if (this.Response.StatusCode == 302&& this.Response.StatusCode == 401
&& this.Response.RedirectLocation.ToLower().Contains("login.aspx"))
{
this.Response.StatusCode = 401;
//or Response.Redirect("Unauthorized.aspx");
}
您还可以检查此> 将未经授权的用户重定向到ASP .Net中的消息页。(microsoft.com)
其他参考资料
https://stackoverflow.com/questions/71959756
复制相似问题