Error in accent words using Httpcookie

Asked

Viewed 145 times

0

My Cookie Retrieval Method is Bringing You Words With Disfigured 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;
    }
  • @Andersonos is the way you solved?

1 answer

0

One solution is to store this information in a Unicode format that can be reversed again, preserving the special characters I suggest URL Encode, through 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.