如何获取名称在 asp.net 中的 cookie

How to fetch cookie with name in asp.net

我需要获取指定名称的 cookie。

如果用户凭据有效,我将按如下方式设置 cookie。

 public ActionResult Login()
    {
        if(user credentials are valid)
        {
         FormsAuthentication.SetAuthCookie("customer", true);
        }
    }

现在在我的其他一些控制器中,我需要查看是否有名称为 "customer" 的 cookie。

  public ActionResult Validate()
    {
        var cookie = Request.Cookies["customer"];
        //here I am getting cookies as null
    }

当我在 chrome 设置中检查 cookie 时,有 cookie 但没有名称为 customer 但它以默认名称存在 .ASPXAUTH

我的要求不是检查用户是否已通过身份验证,我的要求是检查名称为 "customer" 的 cookie 是否存在,然后执行我的操作。

我该如何满足这个要求。

谢谢。

FormsAuthentication.SetAuthCookie("customer", true);

创建具有特定 的表单身份验证 cookie,但 cookie 名称默认为 .ASPXAUTH(您可以更改)。此类 cookie 由 Forms Auth 模块进一步识别。请注意,customer 这里是 cookie 的值而不是它的名称。

但要创建任意名称的 cookie,只需手动创建即可

public ActionResult Login()
{
   HttpCookie cookie = new HttpCookie("customer", "value");
   this.Response.SetCookie( cookie );
}

请注意,具有任意名称 (customer) 的 cookie 不会 被 Forms Auth 模块识别,因此,用户将不会被识别为已通过身份验证。