2012-04-18 14 views
5

मैं HttpModule से सत्र चर का उपयोग कैसे कर सकता हूं?एक्सेस सत्र वैरिएबल HTTPModule

मैं .cs पेज है, जो मैं HttpModule में पहुंच चाहते हैं में सत्र चर निम्नलिखित सेट:

सत्र [ "उपयोगकर्ता नाम"] = "blah"

उत्तर

3

यहाँ का उपयोग करने का एक उदाहरण है HttpModule भीतर सत्र, here पाया:

using System; 
using System.Web; 
using System.Web.Security; 
using System.Web.SessionState; 
using System.Diagnostics; 

// This code demonstrates how to make session state available in HttpModule, 
// regradless of requested resource. 
// author: Tomasz Jastrzebski 

public class MyHttpModule : IHttpModule 
{ 
    public void Init(HttpApplication application) 
    { 
     application.PostAcquireRequestState += new EventHandler(Application_PostAcquireRequestState); 
     application.PostMapRequestHandler += new EventHandler(Application_PostMapRequestHandler); 
    } 

    void Application_PostMapRequestHandler(object source, EventArgs e) 
    { 
     HttpApplication app = (HttpApplication)source; 

     if (app.Context.Handler is IReadOnlySessionState || app.Context.Handler is IRequiresSessionState) { 
     // no need to replace the current handler 
     return; 
     } 

     // swap the current handler 
     app.Context.Handler = new MyHttpHandler(app.Context.Handler); 
    } 

    void Application_PostAcquireRequestState(object source, EventArgs e) 
    { 
     HttpApplication app = (HttpApplication)source; 

     MyHttpHandler resourceHttpHandler = HttpContext.Current.Handler as MyHttpHandler; 

     if (resourceHttpHandler != null) { 
     // set the original handler back 
     HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler; 
     } 

     // -> at this point session state should be available 

     Debug.Assert(app.Session != null, "it did not work :("); 
    } 

    public void Dispose() 
    { 

    } 

    // a temp handler used to force the SessionStateModule to load session state 
    public class MyHttpHandler : IHttpHandler, IRequiresSessionState 
    { 
     internal readonly IHttpHandler OriginalHandler; 

     public MyHttpHandler(IHttpHandler originalHandler) 
     { 
     OriginalHandler = originalHandler; 
     } 

     public void ProcessRequest(HttpContext context) 
     { 
     // do not worry, ProcessRequest() will not be called, but let's be safe 
     throw new InvalidOperationException("MyHttpHandler cannot process requests."); 
     } 

     public bool IsReusable 
     { 
     // IsReusable must be set to false since class has a member! 
     get { return false; } 
     } 
    } 
} 
+0

'OriginalHandler.ProcessRequest (संदर्भ);' 'ProcessRequest' में यदि आपका कोड ** वास्तव में है ** फोन' ProcessRequest () ' – TheGeekZn

+0

किसी को भी पता है कि अगर यह अभी भी मान्य है? मुझे debug.assert में त्रुटियां मिल रही हैं कि यह संदर्भ इस सत्र में उपलब्ध नहीं है। – Martin

3

मैं एक कम आक्रामक दृष्टिकोण सुझाव देते हैं। SetSessionStateBehavior() के साथ अपने मॉड्यूल में सत्र का पर्दाफाश करने के लिए बस एएसपी.NET रनटाइम को मजबूर करें (IRequiresSessionState के कार्यान्वयन केवल IHttpHandlers के लिए माना जाता है)।

public void Init(HttpApplication httpApp) 
{ 
    //SESSION WILL BE AVAILABLE IN ALL EVENTS FROM PreRequestHandlerExecute TO PostRequestHandlerExecute 
    httpApp.PostRequestHandlerExecute += OnPostRequestHandlerExecute; 
    //THIS IS THE IMPORTANT LINE 
    httpApp.Context.SetSessionStateBehavior(SessionStateBehavior.ReadOnly); 
} 
संबंधित मुद्दे