I am in need to fetch cookie with a specified name.
If user credentials are valid, I am setting a cookie as below.
public ActionResult Login()
{
if(user credentials are valid)
{
FormsAuthentication.SetAuthCookie("customer", true);
}
}
Now in my some of my other controller, I am in need to see if there is any cookie with the name "customer".
public ActionResult Validate()
{
var cookie = Request.Cookies["customer"];
//here I am getting cookies as null
}
When I check cookies in chrome settings, there is cookie but not with the name customer but it exists with default name .ASPXAUTH
My requirement is not to check if user is authenticated, my requirement is to check if a cookie with name "customer" exits & then do some my stuff.
How do I address my this requirement.
Thanks.
The
FormsAuthentication.SetAuthCookie("customer", true);
creates the forms auth cookie with specific value but the cookie name is by default .ASPXAUTH
(you can change that). Such cookie is further recognized by the Forms Auth module. Note that customer
here is the cookie's value rather than its the name.
But to create a cookie with arbitrary name, just create it manually
public ActionResult Login()
{
HttpCookie cookie = new HttpCookie("customer", "value");
this.Response.SetCookie( cookie );
}
Mind that a cookie with arbitrary name (customer
) will not be recognized by the Forms Auth module and thus, users will not be recognized as authenticated.