简体   繁体   English

请求Cookie的ASP.NET C#System.NullReferenceException

[英]ASP.NET C# System.NullReferenceException on requesting cookies

I have a problem when I request cookies, but I don't understand what I can do to solve it. 请求Cookie时遇到问题,但是我不知道该怎么办才能解决。

My code: 我的代码:

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!";
        }
    }
}

So I have tried this to fix it: 所以我试着解决这个问题:

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!";
        }
    }
}

But I always get the following error: 但是我总是收到以下错误:

Object reference not set to an instance of an object. 你调用的对象是空的。

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 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. 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

Source Error: 源错误:

An unhandled exception was generated during the execution of the current web request. 当前Web请求的执行期间生成了未处理的异常。 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; 版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET Version:4.0.30319.1016 ASP.NET版本:4.0.30319.1016

How can I solve it? 我该如何解决?

This error happens when you try to use a property or call a method of an object that is null. 当您尝试使用属性或调用对象的方法为null时,会发生此错误。 More details: 更多细节:

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. 简单地使用Visual Studio DEBUGGER可以告诉您该对象正在发生的原因。 Just look at the stack trace and put a debugger on that line. 只需查看堆栈跟踪,然后在该行上放置调试器即可。 Check the objects of that line and see if any one is null and you are trying to use that objects property. 检查该行的对象,看看是否有任何一个为空,并且您正在尝试使用该对象属性。 Handle the same. 处理相同。

Might be wrong but it looks like you've wrongly named one of your cookie items. 可能是错误的,但是您似乎错误地命名了一个Cookie项目。 You call it: 您称它为:

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

But on the next line, you reference the IvoucherCookieAdminPassword . 但是在下一行,您引用IvoucherCookieAdminPassword

So, is it CookieAdminPassword or AdminCookiePassword or both? 那么,它是CookieAdminPassword还是AdminCookiePassword还是两者都是?

This is because you are not setting the value of 这是因为您未设置

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

while you are making check on them in Page_Load function when they contain null,before make a condition you have to set their value. 当它们在Page_Load函数中检查它们是否包含null时,在设置条件之前必须设置它们的值。

to view more deatils on .Equal() please see, 要查看有关.Equal()的更多详细信息,请参阅,
http://msdn.microsoft.com/en-us/library/bsc2ak47(v=vs.110).aspx Read more about HttpRequest.Cookies Property go to .... http://msdn.microsoft.com/zh-cn/library/bsc2ak47(v=vs.110).aspx有关HttpRequest.Cookies属性的更多信息,请访问...。
http://msdn.microsoft.com/en-us/library/system.web.httprequest.cookies(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/system.web.httprequest.cookies(v=vs.110).aspx

Happy Coding....... 编码愉快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM