【问题标题】:Owin Authentication creates a redirect loopOwin 身份验证创建重定向循环
【发布时间】:2016-02-01 15:39:33
【问题描述】:

我有一个 ASP.NET 4.6 WebForms 应用程序,它利用 Identity 2.1 包进行注册和身份验证系统。它使用 Owin 身份验证,而不是 Forms 或 Windows。

我的尝试是不允许匿名用户查看网站的任何页面并将他们重定向到登录页面。这就是为什么我在我的 Web.config 中添加了以下内容(根据 this article):

<system.web>
    <authorization>
      <deny users="?"/>
    </authorization>
    <authentication mode="None" />
</system.web>

<location path="~/Account/Login.aspx">
<system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
</system.web>

从那时起,我在浏览器中运行应用程序时总是会出现重定向循环。我为 MVC 找到了不同的解决方案,但没有为 WebForms 找到。

可能是什么原因以及如何消除它?

这是我在 Startup.Auth.cs 文件中的配置方法:

public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login.aspx"),
            CookieSecure = CookieSecureOption.Always,
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });
    }

【问题讨论】:

  • 我最近也有类似的情况。我的登录屏幕在页面加载方法中有一个注销方法。注销调用重定向到注销页面。这导致了一个循环。

标签: asp.net redirect webforms asp.net-identity owin


【解决方案1】:

将身份验证模式设置为“表单” 并将服务器上的身份验证设置为 Formsauthentication & Anonymous。

等等,我在寻找另一个解决方案:)

【讨论】:

    【解决方案2】:
    <authentication mode="Forms" />
            <authorization>
                <allow users="*" />
            </authorization>
    
    
    
    
    
    
    
    <system.webServer>
        <modules>
            <remove name="FormsAuthentication" />
        </modules>
    </system.webServer>
    

    并删除“&lt;deny users="?"/&gt;”行

    【讨论】:

    • 不,抱歉,这是不可能的。看看这个提及:“我的尝试是不允许匿名用户查看网站的任何页面并将他们重定向到登录页面”。为什么我应该选择 Forms 而不是 Owin?
    • 但这是可能的。您需要在每个函数中都不允许匿名调用以引用登录。然后你可以打开匿名
    【解决方案3】:

    更改位置路径

    <location path="~/Account/Login.aspx">
    

    <location path="Account/Login.aspx">
    

    并删除友好的网址

    【讨论】:

      【解决方案4】:

      将此 XML 添加到您的 Accounts/Web.Config 中

      <system.web>
           <authorization>
               <allow users="*"/>
          </authorization>
      </system.web>
      

      这解决了我的问题。

      请注意,这是您的 Accounts 文件夹中的 Web.Config

      【讨论】:

        【解决方案5】:

        我将这两个配置都添加到了我的 web.config 中

        <authentication mode="None"/>
        
        <authorization>
          <deny users="?" />
        </authorization>
        
        
        <location path="Login.aspx">
            <system.web>
              <authorization>
                <allow users="?" />
              </authorization>
            </system.web>
        </location>
        
        <modules>
          <remove name="FormsAuthentication"/>
        </modules>
        

        您需要将 Login.aspx 更改为您自己的页面,例如 Account/Login.aspx

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-07
          • 2016-07-03
          相关资源
          最近更新 更多