如何检查cookie是否为空

时间:2022-08-24 07:28:16

I need to check if cookie is present with value or not. But I wonder if there is some quick and good way of doing so since if I need to check 3 cookies it seems bad to check with if or try.

我需要检查cookie是否具有值。但是我想知道是否有一些快速而又好的方法可以做到这一点,因为如果我需要检查3个cookie,检查一下是否或者尝试一下似乎是不好的。

Why it does not assign empty string to my variable if cookie is not present? Instead it shows Object reference not set to an instance of an object.

如果cookie不存在,为什么它不给变量分配空字符串?相反,它显示没有设置对象实例的对象引用。

My code (it works, but it seems too big for this task, I think there should be a better way of doing this)

我的代码(它可以工作,但是对于这个任务来说似乎太大了,我认为应该有更好的方法)

// First I need to asign empty variables and I don't like this
string randomHash = string.Empty;
string browserHash = string.Empty;
int userID = 0;

// Second I need to add this huge block of try/catch just to get cookies
// It's fine since I need all three values in this example so if one fails all fails
try
{
    randomHash = Convert.ToString(Request.Cookies["randomHash"].Value);
    browserHash = Convert.ToString(Request.Cookies["browserHash"].Value);
    userID = Convert.ToInt32(Request.Cookies["userID"].Value);
}
catch
{
    // And of course there is nothing to catch here
}

As you can see I have this huge block just to get cookies. What I would like is something like this:

你可以看到,我有一个巨大的块,只是为了得到饼干。我想要的是这样的:

// Gives value on success, null on cookie that is not found
string randomHash = Convert.ToString(Request.Cookies["randomHash"].Value);
string browserHash = Convert.ToString(Request.Cookies["browserHash"].Value);
int userID = Convert.ToInt32(Request.Cookies["userID"].Value);

Edit Maybe I can somehow override the .Value method to my liking?

编辑也许我可以按我的喜好重写。value方法?

1 个解决方案

#1


12  

Just check if the cookie is null:

检查cookie是否为空:

if(Request.Cookies["randomHash"] != null)
{
   //do something
}

NOTE: The "Better" way of doing this is to write good code that is both readable and reliable. It doesn't assign empty string because this is not how C# works, you are trying to call the Value property on a null object (HttpCookie) - you cannot use null objects because there is nothing to use.

注意:这样做的“更好”方式是编写可读和可靠的好代码。它不会分配空字符串,因为这不是c#的工作方式,您正在尝试调用空对象(HttpCookie)上的值属性——您不能使用空对象,因为没有什么可使用的。

Converting to an int you still need to avoid parse errors, but you can use this built in method:

转换成int类型仍然需要避免解析错误,但是可以使用内置方法:

int.TryParse(cookieString, out userID);

which brings on another point? Why are you storing the userID in a cookie? this can be changed by the end user - I don't know how you plan on using this but would I be right to assume this is a big security hole?

这又引出了另一个问题?为什么要将userID存储在cookie中?这可以由最终用户更改——我不知道您打算如何使用它,但我是否正确地假设这是一个很大的安全漏洞?


or with a little helper function:

或者有一个小辅助函数:

public string GetCookieValueOrDefault(string cookieName)
{
   HttpCookie cookie = Request.Cookies[cookieName];
   if(cookie == null)
   {
      return "";
   }  
   return cookie.Value;
}

then...

然后……

string randomHash = GetCookieValueOrDefault("randomHash");

Or with an Extension method:

或使用扩展方法:

public static string GetValueOrDefault(this HttpCookie cookie)
{
   if(cookie == null)
   {
      return "";
   }  
   return cookie.Value;  
}

then...

然后……

string randomHash = Request.Cookies["randomHash"].GetValueOrDefault();

#1


12  

Just check if the cookie is null:

检查cookie是否为空:

if(Request.Cookies["randomHash"] != null)
{
   //do something
}

NOTE: The "Better" way of doing this is to write good code that is both readable and reliable. It doesn't assign empty string because this is not how C# works, you are trying to call the Value property on a null object (HttpCookie) - you cannot use null objects because there is nothing to use.

注意:这样做的“更好”方式是编写可读和可靠的好代码。它不会分配空字符串,因为这不是c#的工作方式,您正在尝试调用空对象(HttpCookie)上的值属性——您不能使用空对象,因为没有什么可使用的。

Converting to an int you still need to avoid parse errors, but you can use this built in method:

转换成int类型仍然需要避免解析错误,但是可以使用内置方法:

int.TryParse(cookieString, out userID);

which brings on another point? Why are you storing the userID in a cookie? this can be changed by the end user - I don't know how you plan on using this but would I be right to assume this is a big security hole?

这又引出了另一个问题?为什么要将userID存储在cookie中?这可以由最终用户更改——我不知道您打算如何使用它,但我是否正确地假设这是一个很大的安全漏洞?


or with a little helper function:

或者有一个小辅助函数:

public string GetCookieValueOrDefault(string cookieName)
{
   HttpCookie cookie = Request.Cookies[cookieName];
   if(cookie == null)
   {
      return "";
   }  
   return cookie.Value;
}

then...

然后……

string randomHash = GetCookieValueOrDefault("randomHash");

Or with an Extension method:

或使用扩展方法:

public static string GetValueOrDefault(this HttpCookie cookie)
{
   if(cookie == null)
   {
      return "";
   }  
   return cookie.Value;  
}

then...

然后……

string randomHash = Request.Cookies["randomHash"].GetValueOrDefault();