2011-12-22 77 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表中的任何記錄時,我想參觀管理文件夾頁 請指導我

感謝

回答

相關問題