Error in accented words using HttpCookie

0

My method of recovering cookies is bringing words with an unconfigured accent.

        //Para gravar um Cookie
    private static void Set(string key, string value)
    {
        var encValue = value;
        var cookie = new HttpCookie(key, value)
        {
            Expires = DateTime.Now.AddMinutes(_cookieDuration),
            HttpOnly = _cookieIsHttp
        };
        _context.Response.Cookies.Add(cookie);
    }

    //Para ler um Cookie 
    public static string Get(string key)
    {
        var value = string.Empty;

        var c = _context.Request.Cookies[key];
        return c != null
                ? c.Value
                : value;
    }
    
asked by anonymous 04.04.2018 / 22:20

1 answer

0

One solution is for you to store this information in a Unicode format that can be reverted again by preserving the special characters I suggest URL Encode, using the methods UrlEncode() and UrlDecode() of System.Web.HttpUtility .

//Para gravar um Cookie
private static void Set(string key, string value)
{
    var encValue = HttpUtility.UrlEncode(value);
    var cookie = new HttpCookie(key, encValue)
    {
        Expires = DateTime.Now.AddMinutes(_cookieDuration),
        HttpOnly = _cookieIsHttp
    };
    _context.Response.Cookies.Add(cookie);
}

//Para ler um Cookie 
public static string Get(string key)
{
    var value = string.Empty;

    var c = _context.Request.Cookies[key];
    return c != null
            ? HttpUtility.UrlDecode(value);
            : value;
}
    
09.11.2018 / 13:42