我在配置授权规则时使用 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>
最佳答案
嗯,我认为它是围绕着这个而来的:
<location path="">
<system.web>
<authorization>
<deny users="*"/>
</authorization>
</system.web>
</location>
我在这里看到的问题来自这个 path="",因为这个信息告诉 UserAgent [浏览器,比如 IE 或 FF 或 Chrome] 阻止这个地址:http://localhost:xxxxx
事实上,它指向您的默认路由:~/Restricted/Default.aspx
您默认拒绝所有用户访问此页面。希望它能为您提供有关如何执行此操作的提示。
关于c# - ASP.Net : Authorization issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3813056/