ASP.NET 웹 사이트에서 쿠키를 삭제하는 방법
내 웹 사이트에서 사용자가 "로그 아웃"버튼을 클릭하면 Logout.aspx 페이지가 코드와 함께로드됩니다 Session.Clear()
.
ASP.NET/C#에서 모든 쿠키가 지워 집니까? 아니면 내 웹 사이트의 모든 쿠키를 제거하기 위해 추가해야하는 다른 코드가 있습니까?
다음과 같이 시도하십시오.
if (Request.Cookies["userId"] != null)
{
Response.Cookies["userId"].Expires = DateTime.Now.AddDays(-1);
}
그러나 사용하는 것도 의미가 있습니다.
Session.Abandon();
많은 시나리오 외에도.
아니요, 쿠키는 각각의 만료 날짜 를 설정 해야만 정리할 수 있습니다 .
if (Request.Cookies["UserSettings"] != null)
{
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
현재 Session.Clear()
:
Session
컬렉션의 모든 키-값 쌍이 제거됩니다.Session_End
이벤트가 발생하지 않습니다.
로그 아웃 중에이 Session.Abandon
방법을 사용하는 Session_End
경우 이벤트에 대한 방법 도 사용해야합니다 .
- 세션 ID가있는 쿠키 (애플리케이션에서 기본적으로 세션 ID 저장소에 쿠키를 사용하는 경우)가 삭제됩니다.
이것이 내가 사용하는 것입니다.
private void ExpireAllCookies()
{
if (HttpContext.Current != null)
{
int cookieCount = HttpContext.Current.Request.Cookies.Count;
for (var i = 0; i < cookieCount; i++)
{
var cookie = HttpContext.Current.Request.Cookies[i];
if (cookie != null)
{
var expiredCookie = new HttpCookie(cookie.Name) {
Expires = DateTime.Now.AddDays(-1),
Domain = cookie.Domain
};
HttpContext.Current.Response.Cookies.Add(expiredCookie); // overwrite it
}
}
// clear cookies server side
HttpContext.Current.Request.Cookies.Clear();
}
}
안타깝게도 "만료"설정이 항상 작동하는 것은 아닙니다. 쿠키는 영향을받지 않았습니다.
이 코드는 저에게 효과적이었습니다.
HttpContext.Current.Session.Abandon();
HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
"ASP.NET_SessionId"
쿠키의 이름은 어디에 있습니까 ? 이것은 실제로 쿠키를 삭제하지는 않지만 나를 위해 충분히 가까운 빈 쿠키로 덮어 씁니다.
다른 사람들이 말했듯이 Session.Abandon을 사용할 때 세션 ID 쿠키가 제거되지 않는다는 점을 지적하고 싶습니다.
세션을 중단해도 세션 ID 쿠키는 사용자의 브라우저에서 제거되지 않습니다. 따라서 세션이 중단되는 즉시 동일한 응용 프로그램에 대한 모든 새 요청은 동일한 세션 ID를 사용하지만 새 세션 상태 인스턴스를 갖게됩니다. 동시에 사용자가 동일한 DNS 도메인 내에서 다른 응용 프로그램을 열면 한 응용 프로그램에서 Abandon 메서드가 호출 된 후에도 사용자의 세션 상태가 손실되지 않습니다.
때로는 세션 ID를 재사용하고 싶지 않을 수 있습니다. 세션 ID를 재사용하지 않을 경우의 결과를 이해하고 있다면 다음 코드 예제를 사용하여 세션을 중단하고 세션 ID 쿠키를 지 웁니다.
Session.Abandon(); Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
이 코드 예제는 서버에서 세션 상태를 지우고 세션 상태 쿠키를 null로 설정합니다. null 값은 브라우저에서 쿠키를 효과적으로 지 웁니다.
http://support.microsoft.com/kb/899918
비밀번호를 쿠키로 저장해서는 안됩니다. 쿠키를 삭제하려면 쿠키를 수정하고 만료하기 만하면됩니다. 실제로 삭제할 수는 없습니다. 즉, 사용자의 디스크에서 제거 할 수 없습니다.
다음은 샘플입니다.
HttpCookie aCookie;
string cookieName;
int limit = Request.Cookies.Count;
for (int i=0; i<limit; i++)
{
cookieName = Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
aCookie.Expires = DateTime.Now.AddDays(-1); // make it expire yesterday
Response.Cookies.Add(aCookie); // overwrite it
}
OP의 질문 제목을 모든 쿠키 삭제로 사용- "웹 사이트에서 쿠키 삭제"
웹 어딘가에서 Dave Domagala의 코드를 발견했습니다. Google Analytics 쿠키도 허용하도록 Dave를 수정했습니다. 웹 사이트에있는 모든 쿠키를 반복하고 모두 삭제했습니다. (개발자 관점에서-새 코드를 기존 사이트로 업데이트하는 것은 사용자가 사이트를 다시 방문하는 문제를 방지하는 좋은 방법입니다.)
먼저 쿠키를 읽고 필요한 데이터를 보유한 다음 아래 루프로 모든 것을 깨끗하게 씻은 후 쿠키를 재설정하는 것과 함께 아래 코드를 사용합니다.
코드:
int limit = Request.Cookies.Count; //Get the number of cookies and
//use that as the limit.
HttpCookie aCookie; //Instantiate a cookie placeholder
string cookieName;
//Loop through the cookies
for(int i = 0; i < limit; i++)
{
cookieName = Request.Cookies[i].Name; //get the name of the current cookie
aCookie = new HttpCookie(cookieName); //create a new cookie with the same
// name as the one you're deleting
aCookie.Value = ""; //set a blank value to the cookie
aCookie.Expires = DateTime.Now.AddDays(-1); //Setting the expiration date
//in the past deletes the cookie
Response.Cookies.Add(aCookie); //Set the cookie to delete it.
}
Addition: If You Use Google Analytics
The above loop/delete will delete ALL cookies for the site, so if you use Google Analytics - it would probably be useful to hold onto the __utmz cookie as this one keeps track of where the visitor came from, what search engine was used, what link was clicked on, what keyword was used, and where they were in the world when your website was accessed.
So to keep it, wrap a simple if statement once the cookie name is known:
...
aCookie = new HttpCookie(cookieName);
if (aCookie.Name != "__utmz")
{
aCookie.Value = ""; //set a blank value to the cookie
aCookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(aCookie);
}
It is 2018 now, so in ASP.NET Core, there is a straight forward built in function. To delete a cookie try this code:
if(Request.Cookies["aa"] != null)
{
Response.Cookies.Delete("aa");
}
return View();
Though this is an old thread, i thought if someone is still searching for solution in the future.
HttpCookie mycookie = new HttpCookie("aa");
mycookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(mycookie1);
Thats what did the trick for me.
Response.Cookies["UserSettings"].Expires = DateTime.Now.AddDays(-1)
참고URL : https://stackoverflow.com/questions/6635349/how-to-delete-cookies-on-an-asp-net-website
'code' 카테고리의 다른 글
Base-64 char 배열의 길이가 잘못되었습니다. (0) | 2020.10.11 |
---|---|
안에 공백이없는 텍스트를 어떻게 줄 바꿈합니까? (0) | 2020.10.11 |
경로에서 액세스 할 수있는 app.js의 전역 변수? (0) | 2020.10.11 |
WebStorm 내에서 어떻게 nodemon을 실행할 수 있습니까? (0) | 2020.10.11 |
Chart.js v2 데이터 세트 레이블 숨기기 (0) | 2020.10.11 |