Cookie не удален

Я воспитал несколько младших людей подо мной прежде. Мой подход варьировался немного на основе человека немного на основе того, как они учились.

Короче говоря, я дал младшим людям маленькие, автономные проекты, когда я мог и дал им относительно установленное время для выполнения задачи. Как только задача была завершена, я рассмотрю их подход, код и решение и сделанные предложения для улучшений или лучшего способа решить проблему. Я думаю этот способ, которым они не чувствуют себя разбитым, будучи частью намного большего проекта.

Hope это помогает немного.

34
задан Brian Tompsett - 汤莱恩 4 July 2015 в 13:18
поделиться

3 ответа

Clearing the cookies of the response doesn't instruct the browser to clear the cookie, it merely does not send the cookie back to the browser. To instruct the browser to clear the cookie you need to tell it the cookie has expired, e.g.

public static void Clear(string key)
{
    var httpContext = new HttpContextWrapper(HttpContext.Current);
    _response = httpContext.Response;

    HttpCookie cookie = new HttpCookie(key) 
        { 
            Expires = DateTime.Now.AddDays(-1) // or any other time in the past
        };
    _response.Cookies.Set(cookie);
}
55
ответ дан 27 November 2019 в 16:40
поделиться

The Cookies collection in the Request and Response objects aren't proxies for the cookies in the browser, they're a set of what cookies the browser sends you and you send back. If you remove a cookie from the request it's entirely server side, and if you have no cookies in the response you're just not going to send any thing back to the client, which won't change the set of cookies in the browser at all.

To delete a cookie, make sure that it is in the response cookie collection, but has an expiration time in the past.

5
ответ дан 27 November 2019 в 16:40
поделиться

Чтобы добавить что-то еще, я также возвращаю значение как null, например

    public static void RemoveCookie(string cookieName)
    {
        if (HttpContext.Current.Response.Cookies[cookieName] != null)
        {
            HttpContext.Current.Response.Cookies[cookieName].Value = null;
            HttpContext.Current.Response.Cookies[cookieName].Expires = DateTime.Now.AddMonths(-1);
        }
    }
4
ответ дан 27 November 2019 в 16:40
поделиться
Другие вопросы по тегам:

Похожие вопросы: