ASP.NET Form Authentication - Form Authentication With Directory

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页面的时候,由于在文件夹外面,所以直接可以访问。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值