![](/img/trans.png)
[英]ASP.NET OWIN OpenID Connect not creating user authentication
[英]Asp.net 4.8 WebForms authorization using Owin OpenId Connect Authentication (app.UseOpenIdConnectAuthentication)
我在 login.microsoftonline.com 和我的应用程序之间遇到无限重定向循环。 我的项目是在 Asp.net 4.8 Web 窗体项目中实现身份验证和授权。 我可以使用默认的 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);
}
}
网页配置
<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
。 我通过更新网络配置来做到这一点:
<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 错误的应用程序页面?
请检查以下解决方法是否有帮助:
通常情况下,如果启用了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 中的消息页面。 (微软网站)
其他参考资料
ASP.NET URL 授权似乎无法与 OIDC(即 Azure AD)很好地互操作。
首先从您的 Web.config 中删除 URL 授权:
<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>
可选地使全局所有页面都需要经过身份验证:
<system.web>
<deny users="?" />
</system.web>
您可以使用<Allow users="?" />
覆盖此行为 <Allow users="?" />
用于特定页面,即登录/注销/错误页面/等。
其次将授权逻辑添加到您的AuthRequired.aspx
页面:
public partial class AuthRequired {
protected void Page_Load(object sender, EventArgs e)
{
Authorization.AuthorizeAuthRequiredPage();
...
}
}
public static class Authorization
{
public static void AuthorizeAuthRequiredPage()
{
if (!Authorized(HttpContext.User))
{
Redirect("/Anauthorized.aspx");
}
}
private static bool Authorized(User user) => { ... }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.