2017-12-01 1 views
-2

현재 세션을 사용하여 응용 프로그램을 제어하고 있지만 지금 쿠키를 사용합니다. 내 질문은 CRUD에서 사용합니다. var user_code = Convert.ToInt32 (HttpContext.Current.Session [ "userId "])); 에 내 사용자를 식별하고 내 DB에 쿠키를 남기려면 어떻게해야합니까?Asp.net MVC 쿠키에 대한 쿠키

+0

'세션'을 사용하지 않아야합니다. MVC에서 [Authentication and Authorization] (https://www.asp.net/mvc/overview/security)의 기초를 이해하는 데 시간을 할애하십시오. –

답변

0

동일한 값으로 세션에 값을 할당하면 쿠키에 할당 할 수 있습니다. 보안을 위해 사용자가 암호화 된 형식으로 로그인 할 때 쿠키를 추가해야합니다.

쿠키 생성 코드.

private HttpCookie CreateCookie() 
{ 
    HttpCookie FooCookies = new HttpCookie("userId"); 
    FooCookies.Value = "hello"; 
    FooCookies.Expires = DateTime.Now.AddHours(1); 
    return FooCookies; 
} 

//some action method 
Response.Cookies.Add(CreateCookie()); 

코드 쿠키에서 읽기 가치. 로그 아웃에

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); 
} 
+0

역할을 저장하기 때문에 FormsAuthenticationTicket을 사용하고 있습니다. 문제가 2 개 있습니다 쿠키 또는 거기 FormsAuthenticationTicket에 그것을 할 수 있습니까? – lorranpalmeira

관련 문제