Я использую следующий код для установки cookie в моем приложении asp.net mvc (С#):
public static void SetValue(string key, string value, DateTime expires)
{
var httpContext = new HttpContextWrapper(HttpContext.Current);
_request = httpContext.Request;
_response = httpContext.Response;
HttpCookie cookie = new HttpCookie(key, value) { Expires = expires };
_response.Cookies.Set(cookie);
}
Мне нужно удалить файлы cookie, когда пользователь нажимает кнопку выхода из системы. Набор cookie не удаляется/удаляется с помощью Clear/Remove. Код выглядит следующим образом:
public static void Clear()
{
var httpContext = new HttpContextWrapper(HttpContext.Current);
_request = httpContext.Request;
_response = httpContext.Response;
_request.Cookies.Clear();
_response.Cookies.Clear();
}
public static void Remove(string key)
{
var httpContext = new HttpContextWrapper(HttpContext.Current);
_request = httpContext.Request;
_response = httpContext.Response;
if (_request.Cookies[key] != null)
{
_request.Cookies.Remove(key);
}
if (_response.Cookies[key] != null)
{
_response.Cookies.Remove(key);
}
}
Я пробовал обе вышеперечисленные функции, но файл cookie существует, когда я пытаюсь проверить существование.
public static bool Exists(string key)
{
var httpContext = new HttpContextWrapper(HttpContext.Current);
_request = httpContext.Request;
_response = httpContext.Response;
return _request.Cookies[key] != null;
}
Что может быть проблемой здесь? или что мне нужно сделать, чтобы удалить/удалить файл cookie?