1.背景
之前写过一篇关于ASP.NET Form认证与授权相关的博客,介绍了Form认证的基本原理,演示了如果做Form认证。这种认证方式其实是有很大的局限性的:
1)对于企业应用,不需要授权即可访问的页面,这种认证方式不支持。
2)对于互联网的应用,通常都是先浏览,然后在需要授权的时候再登录,这种方式不支持。
2.解决方案
为了很好地解决这个问题,我们可以把必须要登录才能访问的页面放到某个文件夹下,然后通过配置web.config的形式配置拒绝所有用户访问,只有登录之后才能访问,而文件夹之外的页面支持所有的用户不登录即可访问。
3.示例
1)工程结构
Admin目录下的文件必须要登录才能访问,所以这个目录下的web.config配置如下:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
外面的页面不需要登录即可访问,所以配置文件如下:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="AdminLogin.aspx"></forms>
</authentication>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</configuration>
访问Admin\News.aspx这个文件,由于没有登录,会立即跳转到登录页面。登录之后即可立即跳转到News.aspx页面。
当我们访问Default.aspx页面的时候,由于在文件夹外面,所以直接可以访问。