【问题标题】:Asp.net 4.0 Forms Authentication and FriendlyUrlsAsp.net 4.0 表单身份验证和 FriendlyUrls
【发布时间】:2013-10-04 10:48:54
【问题描述】:

参考:Microsoft.AspNet.FriendlyUrls

我正在使用表单身份验证和 FriendlyUrls。我有一个名为“Account”的子目录,其中包含文件“Register.aspx”。我需要通过 web.config 授予文件“Register.aspx”的权限并拒绝所有其他文件的权限。我尝试了各种设置,但文件 Register.aspx 没有获得权限。

web.config

<system.web>
    <authentication mode="Forms">
      <forms loginUrl="Account/login" name="LOGIN" defaultUrl="Account/Logged" timeout="15" cookieless="UseDeviceProfile" protection="All" slidingExpiration="true" />
    </authentication>
  </system.web>

  <location path="Account">
    <system.web>
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
  </location>

  <location path="Account/Register">
    <system.web>
      <authorization>
        <allow users="?" />
      </authorization>
    </system.web>

【问题讨论】:

    标签: asp.net webforms friendly-url


    【解决方案1】:

    我在 FriendlyURLs 和 Forms 身份验证(虽然是 OWIN)方面遇到了同样的问题。尝试访问授权内容页面尝试重定向到/Account/Login?returnUrl=%2FAccount%2FLogin,只有重定向陷入无限循环,直到查询字符串超过最大长度!!!我能找到的唯一方法是将登录页面(或任何其他允许匿名访问的页面)放在它自己的文件夹中,并授予对该文件夹而不是页面本身的访问权限。因此,如果我有 /Account/Login,我会添加:

      <system.web>    
        <authorization>
          <deny users="?" />
        </authorization>
      </system.web>
    
      <location path="Account">
        <system.web>
          <authorization>
            <allow users="*" />
          </authorization>
        </system.web>
      </location>
    

    我看到您为 /Account 路径添加了一条规则,但已将其设置为拒绝。不确定您是否已将其更改为“本地允许”...

    虽然我使用的是 MS OWIN 的表单身份验证库,而不是默认的 ASP.Net 内置库,但我希望上述内容也适用于标准库。仅供参考,我的表单身份验证设置如下所示:

        public void ConfigureAuth(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                AuthenticationMode = AuthenticationMode.Active,
                LoginPath = new PathString("/Account/Login"),
                LogoutPath = new PathString("/Account/Logout"),
                ExpireTimeSpan = TimeSpan.FromHours(12),
                SlidingExpiration = true,
                CookieName = "MyCookieName.Session",
                CookieSecure = CookieSecureOption.SameAsRequest,
                // Required for AJAX calls
                CookieHttpOnly = false
            });
        }
    

    【讨论】:

      【解决方案2】:

      试试下面的代码,把 * 改成 ?

      <location path="Account/Register">
          <system.web>
            <authorization>
              <allow users="*" />
            </authorization>
          </system.web>
      

      看到这个很好的解释:http://weblogs.asp.net/gurusarkar/archive/2008/09/29/setting-authorization-rules-for-a-particular-page-or-folder-in-web-config.aspx

      【讨论】:

      • 我已经试过了。但是当访问“帐户/注册”路由时,我被重定向到登录页面。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多