我有一個問題,當我請求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
我該如何解決呢?
你爲什麼以明文形式存儲用戶名/密碼?擔心.... –