看上面的图片!
我想达到这个目的,
在admins文件夹中的文件 不允许匿名访问 如果访问index.aspx页面会重定向到login.aspx进行登陆!
还有other.aspx文件也不允许匿名访问 如果用户匿名访问此页面的话 会重定向到根目录下的login.aspx页面登陆!我在web.config文件中使用location对other.aspx设置 可以完成需求,如下:
<configuration>
<system.web>
<authentication mode="Forms" >
<forms loginUrl="login.aspx"></forms>
</authentication>
</system.web><location path="other.aspx">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
</configuration>但是我想通过下面的方法来实现admins文件夹下的权限控制的时候 就不行了
如果我这样写:
<location path="admins">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
访问index.aspx页面 他会重定向到站点根目录下的login.aspx页面 而我想让他重定向到admins文件夹下的login.aspx如果这样写:
<location path="admins">
<system.web>
<authentication mode="Forms" >
<forms loginUrl="login.aspx"></forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
他就会报错:说是authentication 这个元素只能在根目录下,这是由machine.config文件中定义的。
错误信息如下:
在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误。请高手一定帮帮小弟 !
<authentication mode="Forms" >
<forms loginUrl="admins/login.aspx"> </forms>
</authentication>
</system.web>
2、other.aspx访问时转到login.aspx配置文件设置拒绝other.aspx和admins目录,默认登录页面为login.aspx,并设置允许匿名访问admins/login.aspx:
<configuration>
<system.web>
<authentication mode="Forms">
<forms loginUrl="login.aspx"> </forms>
</authentication>
</system.web> <location path="other.aspx">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
<location path="admins/login.aspx">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
<location path="admins">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
</configuration>在login.aspx页面Page_Load事件里加判断:
string url = Request.QueryString["ReturnUrl"];
if (!string.IsNullOrEmpty(url) && url.StartsWith("/admins/"))
{
Response.Redirect("admins/login.aspx");
}
string url = Request.QueryString["ReturnUrl"];
if (!string.IsNullOrEmpty(url) && url.StartsWith("/admins/"))
{
Response.Redirect("admins/login.aspx?ReturnUrl=" + Server.UrlEncode(Request.QueryString["ReturnUrl"]));
}
<authentication mode="Forms" >
<forms loginUrl="admins/login.aspx"> </forms>
</authentication>
</system.web>