Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc Asp.net MVC会话到Cookie_Asp.net Mvc - Fatal编程技术网

Asp.net mvc Asp.net MVC会话到Cookie

Asp.net mvc Asp.net MVC会话到Cookie,asp.net-mvc,Asp.net Mvc,目前我正在使用会话来控制我的应用程序,我现在将使用cookies,但我的问题是关于我使用的CRUD var user_code=Convert.ToInt32(HttpContext.Current.Session[“userId]”); 要在我的数据库中持久化我的用户和Cookie,该如何做?当您将值分配给会话时,您可以将其分配给Cookie。 为了安全起见,用户以加密格式登录时需要添加cookie 创建Cookie的代码 用于从Cookie中读取值的代码 注销时,您需要删除您创建的此cook

目前我正在使用会话来控制我的应用程序,我现在将使用cookies,但我的问题是关于我使用的CRUD var user_code=Convert.ToInt32(HttpContext.Current.Session[“userId]”);
要在我的数据库中持久化我的用户和Cookie,该如何做?

当您将值分配给会话时,您可以将其分配给Cookie。 为了安全起见,用户以加密格式登录时需要添加cookie

创建Cookie的代码

用于从Cookie中读取值的代码

注销时,您需要删除您创建的此cookie

if ( Request.Cookies["userId"] != null )
{
    var c = new HttpCookie( "userId" );
    c.Expires = DateTime.Now.AddDays( -1 );
    Response.Cookies.Add( c );
}

您不应该使用
会话
。建议您花一些时间了解在MVCI am中使用FormsAuthenticationTicket的基本知识,因为要保存我的角色。在FormsAuthenticationTicket上使用2 cookie或存在任何问题吗?
var cookie = Request.Cookies["userId"];
    if (cookie != null)
    {
        var value = cookie.Value;
        // TODO: do something with the value
    }
if ( Request.Cookies["userId"] != null )
{
    var c = new HttpCookie( "userId" );
    c.Expires = DateTime.Now.AddDays( -1 );
    Response.Cookies.Add( c );
}