2011-12-22 123 views
0

我想保存用户的IP和活动在一个名为logPublic的表中,我想当一个未经认证的用户试图访问一个特殊文件夹,例如管理员文件夹,我可以在logpublic表中添加一个记录,它有一些e,g字段: ID,IP,活动,日期时间。之后unathenticated用户将被锁定utomatically未将对象引用设置为对象的实例。在HttpModule

我在Load_Page事件在管理文件夹中使用下面的代码的母版的:

$public partial class Admin : System.Web.UI.MasterPage 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     if (!HttpContext.Current.User.Identity.IsAuthenticated) 
     { 
      Session["IsBlocked"] = true; 
      string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
      HttpContext.Current.Session["UserIP"] = ip; 
      HttpContext.Current.Session["Activity"] = HttpContext.Current.Request.Url; 
      HttpContext.Current.Session["DateTime"] = System.DateTime.Now; 
     } 
     else 
     { 
      if(! HttpContext.Current.User.IsInRole("Admin")) 
      { 

      Session["BlockUser"] = true; 
      string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
      HttpContext.Current.Session["UserIP"] = ip; 

      } 
     } 


    } 
} 


$namespace StoreProject.Data 
{ 
    public class CustomSecurityModule :IHttpModule 
    { 

    storedbEntities StoreEnt = new storedbEntities(); 
    public void Dispose() 
    { 
     throw new NotImplementedException(); 
    } 

    public void Init(HttpApplication context) 
    { 
     //throw new NotImplementedException(); 
     context.BeginRequest += new EventHandler(this.app_DoSecuriy); 
    } 

    private void app_DoSecuriy(object sender, EventArgs e) 
    { 
     // Create HttpApplication and HttpContext objects to access 
     // request and response properties. 
     HttpApplication application = (HttpApplication)sender; 
     HttpContext context = application.Context; 

     storedbEntities StoreEnt = new storedbEntities(); 

     if (context.Session["BlockUser"]!= null && Convert.ToBoolean(context.Session["BlockUser"])== true) 
     { 

       logPrivate Log = new logPrivate() 
       { 
        Username = context.User.Identity.Name, 
        IP = context.Session["UserIP"].ToString(), 
        Enter = System.DateTime.Now, 

       }; 
       StoreEnt.logPrivates.AddObject(Log); 
       StoreEnt.SaveChanges(); 
       context.Response.Redirect("~/UnAuthorizedAccess.aspx"); 


     } 
     //ublock != null && bool.TryParse(ublock.ToString(),out isblocked) && isblocked 
     else if (context.Session["BlockPublick"] != null 
       && System.Convert.ToBoolean(context.Session["BlockPublick"]) == true) 
     { 

      LogPublic newLog = new LogPublic() 
      { 

       IP = context.Session["UserIP"].ToString(), 
       Activity = context.Session["Activity"].ToString(), 
       Enter = Convert.ToDateTime(context.Session["DateTime"]) 

      }; 
      StoreEnt.LogPublics.AddObject(newLog); 
      StoreEnt.SaveChanges(); 

      context.Response.Redirect("~/UnAuthorizedAccess.aspx"); 
     } 


     } 
    } 
} 

但是当我运行我的应用程序的网站,我从httpmodule中获取错误:对象引用未设置为对象的实例。错误在下面一行

if (context.Session["BlockUser"]!= null 
    && Convert.ToBoolean(
     context.Session["BlockUser"])== true) 

我没有在LogPublic表或logPrivate表中的任何记录时,我想参观管理文件夹页 请指导我

感谢

回答

相关问题