forms身份认证仍然能访问html页面解决办法

asp.net的forms身份认证保护是一个非常棒的东西,用VS2010创建一个Web应用程序即可看到范例

在web.config中配置

          <authentication mode="Forms" >

            <forms loginUrl="~/Account/Login.aspx" protection="All" timeout="2880" />

        </authentication>

这样就可以指定验证失败后跳转到页面,然后再你想验证的Account文件下面再放一个Web.config

<configuration>


  <location path="Register.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>


  <system.web>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>


</configuration>

这样就表示除了account文件下的Register.aspx可以不用通过身份验证外,其他的都必须通过身份验证。

但是这里有个问题。我在account下放了个1.html的文件。在VS中浏览这个文件时,会被强行跳转到登录页。但是我把这个项目发布之后,在IIS 却可以直接浏览这个html文件。

在身份验证的配置中,protection="All",会保护所有的aspx文件,但是html并不会保护。所以,要想IIS中html文件得到forms的身份验证必须配置html文件以aspx文件的方式工作。

配置分两步:

1.在网站的web.config下增加: <httpHandlers>  <add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory"  validate="true" />    </httpHandlers>放在<system.web>下。同时在<compilation debug="true" targetFramework="4.0">下添加 <buildProviders> <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
          </buildProviders>

2。进入iis中的网站,属性--->主目录--->配置:在应用程序那里可以看到已经配置好的.aspx文件。点击添加:"可执行文件",创建一个.html,填写方式和.aspx一样,自己负责粘贴。

配置好之后,.html文件就会以.aspx文件的方式工作。

再次浏览网站时,account文件下的1.html同样需要身份验证才能访问了。

转载自:https://blog.csdn.net/yinffer/article/details/6602148

猜你喜欢

转载自www.cnblogs.com/ITzhangyunpeng/p/9177797.html