【发布时间】: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>
我面临的问题是:
当我尝试访问 http://localhost/ 时——由于我在 Global.asax 中的 dashboard 规则,而不是被重定向到 http://localhost/login/?ReturnUrl=%2f,我我实际上正在获取 http://localhost/Restricted/Default.aspx 页面的内容。
当我尝试访问http://localhost/Restricted/ 时——我确实被重定向到http://localhost/login/?ReturnUrl=%2fRestricted——这是一个好兆头!
知道发生了什么吗?
编辑 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