首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Asp.net 4.8使用Owin OpenId连接身份验证(app.UseOpenIdConnectAuthentication)的WebForms授权

Asp.net 4.8使用Owin OpenId连接身份验证(app.UseOpenIdConnectAuthentication)的WebForms授权
EN

Stack Overflow用户
提问于 2022-04-21 19:08:26
回答 1查看 1.4K关注 0票数 4

我遇到了login.microsoftonline.com和我的应用程序之间的无限重定向循环。我的项目是在Asp.net 4.8WebForms项目中实现身份验证和授权。我能够使用默认的Owin启动文件添加身份验证,然后在web配置文件中要求身份验证。下面的操作正确地要求用户在能够访问pages/AuthRequired之前登录

StartupAuth.CS

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
<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配置来做到这一点的:

代码语言:javascript
运行
复制
<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错误的应用程序页面?

EN

回答 1

Stack Overflow用户

发布于 2022-04-24 13:22:35

请检查下面的工作是否有帮助:

通常情况下,如果启用了forms authentication,当状态代码为401时,您将被重定向到登录页面。

作为一种解决方法,尝试在应用程序结束请求中将下面的内容添加到global.asax中,如果需要,您可以创建自己的未经授权的页面并重定向到该页面。

代码语言:javascript
运行
复制
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)

其他参考资料

  1. 防止重定向登录状态代码401 (未经授权) (microsoft.com)
  2. 未经授权的401 asp.net就地处理(不重定向)?-堆栈溢出
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71959756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档