none
Usar Session em Module RRS feed

  • Pergunta

  • Saudações.

    Estou usando ess Module, mas preciso colocar uma Session aí. E dá erro dizendo que context.Session é nulo. O que posso fazer?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using Hermes.Areas.Seguranca.Models;
    using Hermes.Areas.Seguranca.BFL;

    namespace Hermes
    {
        public class AccessControl : IHttpModule
        {
            public void BeginRequest(object source, EventArgs e)
            {
                HttpContext context = ((HttpApplication)source).Context;

                //Pega o cookie com as informações de login
                HttpCookie cookie = context.Request.Cookies[FormsAuthentication.FormsCookieName];

                string currentURL = context.Request.Path.ToUpper();

                if(!currentURL.Contains("."))
                {
                    if (cookie != null)
                    {
                        MembershipUser user = Membership.GetUser(FormsAuthentication.Decrypt(cookie.Value).Name);

                        List<AreaAcesso> areas = AreaAcessoFlow.ObterAreaAcessoPorGrupo(string.Join(",", Roles.GetRolesForUser(user.UserName)));

                        foreach (AreaAcesso a in areas)
                        {
                            if (a.LinkText == currentURL)
                            {
                            }
                        }
                    }
                }
            }

            public void Dispose()
            {
            }

            public void Init(HttpApplication context)
            {
                context.BeginRequest += BeginRequest;
            }
        }
    }


    Desenvolva!!!

    quinta-feira, 28 de junho de 2012 16:22

Respostas

  • Veja um ex

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


    Não esqueça de usar o componente </> na barra para posta seu código. Microsoft MCPD,MCTS,MCC

    • Marcado como Resposta Harley Araujo segunda-feira, 2 de julho de 2012 10:48
    quinta-feira, 28 de junho de 2012 17:08
    Moderador