【问题标题】:ASP .Net: Authorization issueASP .Net:授权问题
【发布时间】:2011-04-18 07:16:33
【问题描述】:

当我在配置授权规则时使用 ASP .Net 4 的 URL 路由功能时遇到了一些问题。

Global.asax

void Application_Start(object sender, EventArgs e) {
    RegisterRoutes(RouteTable.Routes);
}

private void RegisterRoutes(RouteCollection routes) {
    routes.MapPageRoute("dashboard", "", "~/Restricted/Default.aspx", true);
    routes.MapPageRoute("register", "register", "~/Register.aspx", true);
    routes.MapPageRoute("login", "login", "~/Login.aspx", true);
}

{Root}\Web.Config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.web>
        <authentication mode="Forms">
            <forms name="DevAuth" 
                   loginUrl="/login/" 
                   protection="All" 
                   path="/" 
                   timeout="15"
                   requireSSL="false" 
                   slidingExpiration="true" 
                   cookieless="AutoDetect" />
        </authentication>
    </system.web>
    <system.webServer>
        <security>
            <authentication>
                <basicAuthentication enabled="false" />
            </authentication>
        </security>
    </system.webServer>
</configuration>

{Root}\Restricted\Web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <security>
            <authorization>
                <remove users="*" roles="" verbs="" />
                <add accessType="Allow" roles="Developer" />
                <add accessType="Deny" users="*" />
            </authorization>
        </security>
    </system.webServer>
</configuration>

我面临的问题是:

知道发生了什么吗?

编辑 1

配置文件中的以下更改使我访问被拒绝。

{Root}\Web.Config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.web>
        <authentication mode="Forms">
            <forms name="DevAuth" 
                   loginUrl="/login/" 
                   protection="All" 
                   path="/" 
                   timeout="15"
                   requireSSL="false" 
                   slidingExpiration="true" 
                   cookieless="AutoDetect" />
        </authentication>
    </system.web>
    <system.webServer>
        <security>
            <authentication>
                <basicAuthentication enabled="false" />
            </authentication>
        </security>
    </system.webServer>
    <location path="login">
        <system.web>
            <authorization>
                <allow users="*"/>
            </authorization>
        </system.web>
    </location>
    <location path="register">
        <system.web>
            <authorization>
                <allow users="*"/>
            </authorization>
        </system.web>
    </location>
    <location path="">
        <system.web>
            <authorization>
                <deny users="*"/>
            </authorization>
        </system.web>
    </location>
</configuration>

【问题讨论】:

  • 我在根 web.config 中没有看到授权部分,如果要自动重定向,请尝试在根上添加拒绝规则
  • @curtisk:请检查我上面的编辑 1。

标签: c# asp.net url-rewriting forms-authentication asp.net-4.0


【解决方案1】:

嗯,我认为它是围绕这个来的:

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

我在这里看到的问题来自这个 path="",因为这个信息告诉 UserAgent [Browser, like IE or FF or Chrome] 来阻止这个地址:http://localhost:xxxxx

实际上,它指出了您的默认路由:~/Restricted/Default.aspx

默认情况下,您拒绝所有用户访问此页面。希望它能为您提供有关如何执行此操作的提示。

【讨论】:

    【解决方案2】:

    您实际上并没有使用 URL 重写;您正在使用路由。两者之间的显着差异可能会导致您遇到麻烦:使用路由,您请求的 URL 永远不会更改。所以授权系统仍然根据在地址栏中输入的 URL 进行工作……它对路由引擎在做什么一无所知。

    这完美地解释了你的初始行为;根据您的初始身份验证规则,允许请求根/默认值(空字符串路由值)。路由导致~/Restricted/Default.aspx 成为加载的内容这一事实无关紧要——也就是说,它被忽略了。同样,直接请求 /Restricted/ 会触发 auth 机制。

    出于这个原因,路由和基于文件/位置的授权实际上很难一起使用。

    另一方面,如果您使用重写(更改请求的实际 URL),事情会按照您的预期工作。

    【讨论】:

      【解决方案3】:

      正如 Andrew Barber 所写,当您以这种方式使用路由时,您的身份验证规则将不会发挥作用。

      您可以在此处阅读有关路由和身份验证/授权的更多信息:http://blogs.msdn.com/b/mikeormond/archive/2008/06/21/asp-net-routing-and-authorization.aspx..

      【讨论】:

        猜你喜欢
        • 2021-04-13
        • 1970-01-01
        • 1970-01-01
        • 2022-08-17
        • 1970-01-01
        • 1970-01-01
        • 2019-12-20
        • 2015-08-02
        • 1970-01-01
        相关资源
        最近更新 更多