简体   繁体   English

ASP.NET WebForms Url授权和无限循环。 Web.config无法正常工作?

[英]ASP.NET WebForms Url authorization and an infinite loop. Web.config not working?

I've created a website which uses ASP.NET Identity for user account functionality. 我创建了一个网站,该网站使用ASP.NET Identity来实现用户帐户功能。 I want to restrict access to all pages in a specific folder ("Account") except "Login" and "Register" in my application using standard url authorization. 我想使用标准的URL授权来限制对应用程序中除“登录”和“注册”之外的特定文件夹(“帐户”)中所有页面的访问。 Not logged-in users should be able to open only "Account/Login" and "Account/Register" and those authenticated should be able to open everything else except those pages. 未登录的用户应该只能打开“帐户/登录”和“帐户/注册”,而经过身份验证的用户应该可以打开除那些页面之外的其他所有内容。

The root Web.config has no authorization rules and a Web.config which I put in the Account folder has that: 根Web.config没有授权规则,而我放入Account文件夹中的Web.config具有:

<?xml version="1.0"?>
<configuration>

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

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

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

</configuration>

With those rules, however, there is a problem. 但是,有了这些规则,就会出现问题。 Requesting any page in that folder, including "Login.aspx", causes a redirection to http://localhost:15284/Account/Login . 请求该文件夹中的任何页面,包括“ Login.aspx”,都会导致重定向到http://localhost:15284/Account/Login As I said, even requesting the "Login" page redirects back to itself just like the user wasn't allowed so an infinite loop is created. 就像我说的那样,即使请求“登录”页面也被重定向回自身,就像不允许用户那样,因此创建了无限循环。 That loop causes HTTP 404.15, because the query string exceeds its length limit ("?ReturnUrl=%2FAccount%2FLogin" is appended to the URL on every redirection). 该循环导致HTTP 404.15,因为查询字符串超出了其长度限制(每次重定向时都会在URL上附加“?ReturnUrl =%2FAccount%2FLogin”)。

Are my rules incorrect or this is something else? 我的规则不正确吗?还是其他? Perhaps the problem is somehow related to ASP.NET Identity? 也许问题与某种程度上与ASP.NET Identity有关? Or maybe this is happening because of url rewriting (enabled by default in VS 2013 WebForms template)? 还是因为URL重写(在VS 2013 WebForms模板中默认启用)而发生这种情况?

Without that Web.config the website of course works but everyone has access to everything which is not really something I want. 没有该Web.config,该网站当然可以运行,但是每个人都可以访问我真正想要的所有内容。

Thanks in advance and sorry for my English! 在此先感谢您,感谢您的英语! :) :)

Try 尝试

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

This should allow access to unauthenticated users 这应该允许未经身份验证的用户访问

Use the folder "account", or create another one in which may insert the pages that you want accessible from anonymous user, with its owner web.config that contain the following configuration: 使用文件夹“帐户”,或创建另一个文件夹,在其中可以插入您希望从匿名用户访问的页面,其所有者为web.config,其中包含以下配置:

<configuration>
    <system.web>
        <authorization>
            <allow users="?"/>
        </authorization>
    </system.web>
</configuration>

As you can see without using "location" tag 如您所见,没有使用“位置”标签

    <location path="Login.aspx">

whereas in the root web.config in which it remains this: 而在根web.config中,它仍然是:

<authorization>
    <deny users="?"/>
</authorization> 

This is a workaround because as explained in this article: http://msdn.microsoft.com/en-us/library/wce3kxhd.aspx "Rules contained in application-level configuration files take precedence over inherited rules. The system determines which rule takes precedence by constructing a merged list of all rules for a URL, with the most recent rules (those nearest in the hierarchy) at the head of the list" 这是一种解决方法,因为如本文中所述: http : //msdn.microsoft.com/zh-cn/library/wce3kxhd.aspx “应用程序级配置文件中包含的规则优先于继承的规则。系统确定哪个规则通过构造URL的所有规则的合并列表来获得优先级,最新的规则(层次结构中最接近的规则)位于列表的顶部”

A bit old thread, but I hope this helps, at least someone. 线程有点旧,但是我希望这至少对某人有帮助。 The redirect to /Account/Login comes from Startup.cs, which by default is somewhat the following: 重定向到/ Account / Login来自Startup.cs,默认情况下,它是以下内容:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });            
    }
}

So, replace the LoginPath = new PathString("/Account/Login") with your login path. 因此,用您的登录路径替换LoginPath = new PathString("/Account/Login")

I struggled with the same problem for a while, but it works now.. 我在同一个问题上苦苦挣扎了一段时间,但现在可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM