如何将cookie作为方法参数

Moh*_*ein -1 c# asp.net-mvc-3

我得到cookiecontroller,我想传递cookieChekLogin对方法 login.cs是什么饼干的种类上ChekLogin

public ActionResult test()
{
  Login.ChekLogin(Request.Cookies["Account"];
}
Run Code Online (Sandbox Code Playgroud)

checklogin方法

public static bool ChekLogin()// what is type of cookie
{
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 8

Request.Cookies["Account"]返回一个HttpCookie,这是您的CheckLogin方法可以作为参数的类型:

public static bool ChekLogin(HttpCookie cookie)
{
    if (cookie != null)
    {
        string cookieValue = cookie.Value;
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,如果请求中不存在cookie,Request.Cookies["Account"]则返回null,因此请确保在ChekLogin方法中考虑到这一点.

此外,只是为了确保您不会重新发明某些轮子或打开您的网站以应对安全风险,请确保您已阅读过Forms Authentication in ASP.NET.

  • +1用于保留`Chek`的拼写. (2认同)