2014-01-08 38 views
0

我有一個問題,當我請求cookies時,但我不明白我能做些什麼來解決它。ASP.NET C#System.NullReferenceException請求Cookie

我的代碼:

public partial class Admin_LogIn : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Request.Cookies["IvoucherCookieAdminPassword"] != null && Request.Cookies["IvoucherCookieAdminMail"] != null) 
      if ("***".Equals(Request.Cookies["IvoucherCookieAdminPassword"].Value.ToString()) && "***".Equals(Request.Cookies["IvoucherCookieAdminMail"].Value.ToString())) 
       Response.Redirect("Benvenuto.aspx"); 
    } 
    protected void LoginButton_Click(object sender, EventArgs e) 
    { 
     if (UserTextBox.Text == "***" && PasswordTextBox.Text == "***") 
     { 
      Response.Cookies["IvoucherCookieAdminMail"].Value = UserTextBox.Text; 
      Response.Cookies["IvoucherAdminCookiePassword"].Value = PasswordTextBox.Text; 
      Response.Cookies["IvoucherCookieAdminMail"].Expires = DateTime.Now.AddHours(2); 
      Response.Cookies["IvoucherAdminCookiePassword"].Expires = DateTime.Now.AddHours(2); 
      Response.AddHeader("REFRESH", "0.1;Benvenuto.aspx"); 
     } 
     else 
     { 
      GeneralErrorTextBox.Text = "Nome utente e/o password errata!"; 
     } 
    } 
} 

所以我曾嘗試這個修復它:

public partial class Admin_LogIn : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     var adminuser = Request.Cookies["IvoucherCookieAdminMail"]; 
     var adminpassword = Request.Cookies["IvoucherAdminCookiePassword"]; 
     if (adminuser != null && adminpassword != null) 
      if ("***".Equals(Request.Cookies["IvoucherCookieAdminPassword"].Value.ToString()) && "***".Equals(Request.Cookies["IvoucherCookieAdminMail"].Value.ToString())) 
       Response.Redirect("Benvenuto.aspx"); 
    } 
    protected void LoginButton_Click(object sender, EventArgs e) 
    { 
     if (UserTextBox.Text == "***" && PasswordTextBox.Text == "***") 
     { 
      Response.Cookies["IvoucherCookieAdminMail"].Value = UserTextBox.Text; 
      Response.Cookies["IvoucherAdminCookiePassword"].Value = PasswordTextBox.Text; 
      Response.Cookies["IvoucherCookieAdminMail"].Expires = DateTime.Now.AddHours(2); 
      Response.Cookies["IvoucherAdminCookiePassword"].Expires = DateTime.Now.AddHours(2); 
      Response.AddHeader("REFRESH", "0.1;Benvenuto.aspx"); 
     } 
     else 
     { 
      GeneralErrorTextBox.Text = "Nome utente e/o password errata!"; 
     } 
    } 
} 

但我一直得到以下錯誤:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] 
    Admin_LogIn.Page_Load(Object sender, EventArgs e) +126 
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 
    System.Web.UI.Control.OnLoad(EventArgs e) +91 
    System.Web.UI.Control.LoadRecursive() +74 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207 

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1016

我該如何解決呢?

+0

你爲什麼以明文形式存儲用戶名/密碼?擔心.... –

回答

0

當您嘗試使用屬性或調用null對象的方法時,會發生此錯誤。更多細節:

Visual Studio DEBUGGER的一個簡單的使用可以告訴你這個對象,因爲它正在發生。只需看一下堆棧跟蹤並在該行上放置一個調試器即可。檢查該行的對象,看看是否有任何一個爲null,並且您正嘗試使用該對象屬性。處理相同。

+0

它對我說,這是在其中存在 「***」。等於(Request.Cookies [「IvoucherCookieAdminPassword」],Value.ToString())&&「***」。 (Request.Cookies [「IvoucherCookieAdminMail」]。Value.ToString()) 但在它們通過「if(adminuser!= null && adminpassword!= null)」之前 – ProtoTyPus

0

可能是錯誤的,但它看起來像你錯誤地命名你的cookie項目之一。你稱之爲:

var adminpassword = Request.Cookies["IvoucherAdminCookiePassword"]; 

但是在下一行中,您引用了IvoucherCookieAdminPassword

那麼,它是CookieAdminPassword還是AdminCookiePassword或兩者兼而有之?

0

這是因爲當你對他們做檢查的的Page_Load功能時,它們包含空值,之前讓你必須設置其值的條件,你不設置的

Request.Cookies["IvoucherCookieAdminPassword"].Value **and** Request.Cookies["IvoucherCookieAdminMail"].Value 

值。

查看更多deatils上.Equal()請看,
http://msdn.microsoft.com/en-us/library/bsc2ak47(v=vs.110).aspx 瞭解更多關於HttpRequest.Cookies物業去....
http://msdn.microsoft.com/en-us/library/system.web.httprequest.cookies(v=vs.110).aspx

編碼愉快.......