Windows身份验证初始化ASP.net

时间:2013-10-23 12:01:45

标签: asp.net webforms windows-authentication

在ASP.net WebForms项目中,我们目前使用Forms Authentication,但客户端希望使用Windows身份验证。所以我已经将web.config更改为使用Windows身份验证。但是,应用程序需要一些用户输入并在访问任何网页之前输入会话。我们目前在登录页面的回发中执行此操作。

由于Windows身份验证没有“登录”页面,如何实现?如果会话设置正确,我应该检查每个页面的On_Init事件吗?

2 个答案:

答案 0 :(得分:1)

如果您需要在每个页面中提供会话中的特定数据,最简单的方法是使用专用的模块来检查会话可用的早期管道事件之一中的条件(获取请求状态事件听起来最合适。)

public class CustomConditionCheckModule : IHttpModule
{
    public void Init( HttpApplication context )
    {
        context.AcquireRequestState += new EventHandler( acq_req_state );
    }        

    public void acq_req_state( object sender, EventArgs e )
    {
        // check your condition there - the data is in session
        var session = HttpContext.Current.Session;
        if ( ... the condition ... )
           Response.Redirect( "~/anonymous.page.aspx" ); 
    }
}

然后您还需要一个可以匿名访问的页面(您需要web.config中的一个部分):

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

答案 1 :(得分:-1)

我认为您可以使用 LDAP (Active Directory身份验证)  使用web.config中的Windows身份验证,但是如果是其Intranet Web应用程序或其他方式,即使用Windows身份验证的基于角色的安全性