Intranet user windows Authentication, external user Form Authentication

经常有这样的需求,一个web application 公司内部域用户访问时自动登录,外部用户访问时需要提供用户名和密码用Form登录。

1. 在web项目中新建  WinLogin.aspx, WebLogin.aspx,Redirect401.htm

在Redirect401.htm中添加javascript 代码将页面跳转到WebLogin.aspx上 

2. 将web application的web.config配置成form登录

  <authentication mode="Forms">
            <forms  loginUrl="Winlogin.aspx"  ></forms>
      </authentication>

3. pulish到iis上后 打开IIS Manager right-click WinLogin.aspx -> properities -> File Security tab ->edit the authenticaion

un-check Enable anonymous access and check Integrated Windows authentication

4. 在IIS Manager中 right-click WebLogin.aspx -> properities -> File Security tab ->edit the authenticaion. only check enable anonymous access

5. make sure 打开IE -> internet options ->security ->custom level -> user authentication the "automatic logon only int Intranet zone" or "automatic logon with current user name and password" checked

6. 打开IIs manager right-click the web application -> properities -> custom errors reset 401 httperrors contents direct to  Redirect401.htm

7. 用户访问web application时页面会跳转带 Winlogin.aspx,如果该用户是域用户会自动登录,如果是外部用户ie会弹出登录框。外部用户登录失败或者或者取消登录时会产生http 401 error 进而跳转到Redirect401.htm,而Redirect401.htm中的javascript code 又将页面跳转到WebLogin.aspx,因为WebLogin.aspx在iis中设置成可以匿名访问所以就不会自动跳转了。

 参考自 http://msdn.microsoft.com/en-us/library/ms972958.aspx



原文地址:https://www.cnblogs.com/kting/p/2272336.html