asp.net Forms 身份验证 html页面

来源:互联网 发布:js 类似sleep 编辑:程序博客网 时间:2024/04/29 08:34

IIS 中默认是不支持静态页面进行Forms验证,在网上找个了好多答案,配置都太过繁琐

想让IIS的Forms验证 支持HTML静态页面其实只需要在web.config下进行简单的配置就可以了


1、在  system.web节点 配置  Forms验证

 <system.web>      <authentication mode="Forms">      <forms loginUrl="Web/login.html" timeout="100" protection="All" />    </authentication>  </system.web>  


2、在  configuration 节点下 增加 location节点 声明需要验证的文件
 <location path="Web/Views">    <system.web>      <authorization>        <deny users="?"/>      </authorization>    </system.web>  </location>
此时未登录的状态下 访问Views文件下的aspx,cshtml等.net的资源文件就会重定向至Web/login.html 页面,但是访问html、xml等文件还是不需要验证。

3、在system.webServer节点增加 handlers  节点配置

 <system.webServer>    <handlers>      <add name="htmlPageHandlerFactory" path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" preCondition="integratedMode" />    </handlers>  </system.webServer>
name: 随便定义,但是必须唯一

path :*表示匹配所有文件,*.html 只匹配html文件

verb 请求谓词 get、post 。 *号表示支持所有谓词


此时再次访问站点的 html文件就会出现如下图的错误



提示已经很明显,只需要在compilation节点下注册一个.html的扩展

<system.web>      <authentication mode="Forms">      <forms loginUrl="Web/login.html" timeout="100" protection="All" />    </authentication>    <compilation debug="true" targetFramework="4.5" defaultLanguage="C#">      <buildProviders>        <add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>      </buildProviders>    </compilation>  </system.web>


当然 最重要的一步不能忘记,登录成功之后设置凭证

        [HttpGet]        public ActionResult Login()        {            System.Web.Security.FormsAuthentication.SetAuthCookie(Guid.NewGuid().ToString(), true);            return Json(new { result = true, msg = "登录成功" }, JsonRequestBehavior.AllowGet);        }




0 0
原创粉丝点击