I wrote the following code to expire all the cookies
in my request
. But along with expiring them, I need to set the domain
property for some of the cookies
explicitly. I was wondering if there is a better way to do the same...
protected void Page_Load(object sender, EventArgs e)
{
Session.Abandon();
HttpCookie cookie;
string cookieName;
int cookieCount = Request.Cookies.Count;
for (int i = 0; i < cookieCount; i++)
{
cookieName = Request.Cookies[i].Name;
cookie = new HttpCookie(cookieName);
cookie.Expires = DateTime.Now.AddDays(-1);
if (string.Compare(cookieName, "cookie1") == 0
|| string.Compare(cookieName, "cookie2") == 0
|| string.Compare(cookieName, "cookie3") == 0
|| string.Compare(cookieName, "cookie4") == 0)
{
cookie.Domain = ".mydomain.com";
}
Response.Cookies.Add(cookie);
}
Response.Redirect("my redirect link");
}
I am new to C# so not sure if there is any other better way to set the domain
value to the selected list
of cookies
only. I am from scripting background, basically from tcl
and there usually I can simply check if the given item is in the list or not like:
if ($item in $list){
// Do something
}
which is similar to performing lsearch
.
Anyways, the reason I don't like the logical operators in if
is because in future i might have more cookies
in the list
to perform the same action and I don't want to keep stuffing the if
condition checks.
I can probably do the same here as tcl
like have a list
of all the cookie names and then in for
loop check for each cookie
name in the list
, but not sure how expensive it is.
Any comments would be appreciated.