Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

webforms - Windows Authentication Initialization ASP.net

In a ASP.net WebForms project, we currently use Forms Authentication, but the client wishes to use Windows Authentication instead. So I've changed the web.config to use Windows authentication. However, the application needs some user input and put in into a session before any webpage can be accessed. We currently do this in the postback of the loginpage.

Since Windows authentication does not have a 'Login' page, how can this be achieved? Should I check on every page it's On_Init event if the session has been set correctly..?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you need a specific data in session available in every single page, the easiest approach would be to have a dedicated module that checks the condition in one of early pipeline events where the session is available (the acquire request state event sounds most suitable).

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" ); 
    }
}

Then you also need a page that can be accessed anonymously (you need a section in the web.config for this):

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...