日期:2014-05-17  浏览次数:20558 次

在bll层中的类如何获取session中的值
在这个类中为什么session总是为null,无论是否登录了
C# code

public class UserAuthorizationModule : IHttpModule
    {
        #region IHttpModule 成员
        public void Dispose()
        {
        }
        public void Init(HttpApplication application)
        {           
            application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
        }
         private void Application_BeginRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;


            string requestUrl = context.Request.Url.ToString();
            //context.Response.Write("<script>alert("+requestUrl+")</script>");
            string requestPage = requestUrl.Substring(requestUrl.LastIndexOf('/') + 1);
            if (requestPage != "UserLogin.aspx")
            {
                if (requestPage == "CreatTable.aspx")
                {
                    // if (context.Session == null || context.Session["username"].ToString().Trim() == "")
                    try
                    {
                        if (context.Session["username"].ToString() == "")
                        {
                            context.Response.Redirect("~/UI/RolesManager/UserLogin.aspx");
                        }
                        else
                        {
                            string userName = context.Session["username"].ToString();
                            string url = context.Request.Url.ToString();
                            // 如果用户没有被授权,请求被终止,并打印提示信息。
                            //if (!CanUseModule(userName, url))
                            //{
                            if (true)
                            {
                                // application.Response.BinaryWrite();
                                application.CompleteRequest();
                                //context.CompleteRequest();
                                context.Response.Write("<script>alert(\"对不起,您无权访问此模块\")</script>");
                                //重新定向到需要转到的页面
                            }
                        }
                    }
                    catch (NullReferenceException)
                    {
                        context.Response.Redirect("~/UI/RolesManager/UserLogin.aspx");
                    }
            }
           
               
                
            } 
        }



------解决方案--------------------
public class UserAuthorizationModule : IHttpModule, IRequiresSessionState
------解决方案--------------------
感觉直接放在App_Code就可以啦。毕竟需要用到Session吧。
例如俺的:
C# code

public class ValidateCookie:IHttpModule
{
    public ValidateCookie()
    {
       
    }

    #region IHttpModule 成员
    public void Dispose()
    { }
    public void Init(HttpApplication context)
    {
        context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
    }
    public void context_AcquireRequestState(object sender, EventArgs e)
    {
        // 获取应用程序
        HttpApplication application = (HttpApplication)sender;
        // 检查用户是否已经登录
        if (application.Context.Session == null)
            return;
        if (application.Context.Session["username"] == null)
        {
            // 获取Url
            string requestUrl = application.Request.Url.ToString();
            string requestPage = requestUrl.Substring(requestUrl.LastIndexOf('/') + 1);
            // 如果请求的页面不是登录页面,刚重定向到登录页面。
            if (requestPage.IndexOf("ShowImage.aspx")!=0)
            {
                if (requestPage