c # Получить файл cookie httponly - PullRequest
2 голосов
/ 17 июня 2010

Как я могу получить файл cookie httponly в httpwebresponse?Обычно я использую CookieContainer для получения файлов cookie в httpwebresponse, но он не работает с файлом cookie httponly.

Есть ли другой способ их перехватить?

Ответы [ 2 ]

6 голосов
/ 20 сентября 2012

Да, возможно получить HTTPOnly cookie , например, из клиентской программы, используя функцию "InternetGetCookieEx" в "Wininet.dll" . Вы должны использовать PInvoke код, подобный следующему:

/// <summary>
/// WinInet.dll wrapper
/// </summary>
internal static class CookieReader
{
    /// <summary>
    /// Enables the retrieval of cookies that are marked as "HTTPOnly". 
    /// Do not use this flag if you expose a scriptable interface, 
    /// because this has security implications. It is imperative that 
    /// you use this flag only if you can guarantee that you will never 
    /// expose the cookie to third-party code by way of an 
    /// extensibility mechanism you provide. 
    /// Version:  Requires Internet Explorer 8.0 or later.
    /// </summary>
    private const int INTERNET_COOKIE_HTTPONLY = 0x00002000;

    [DllImport("wininet.dll", SetLastError = true)]
    private static extern bool InternetGetCookieEx(
        string url,
        string cookieName,
        StringBuilder cookieData,
        ref int size,
        int flags,
        IntPtr pReserved);

    /// <summary>
    /// Returns cookie contents as a string
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static string GetCookie(string url)
    {
        int size = 512;
        StringBuilder sb = new StringBuilder(size);
        if (!InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
        {
            if (size < 0)
            {
                return null;
            }
            sb = new StringBuilder(size);
            if (!InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
            {
                return null;
            }
        }
        return sb.ToString();
    }
}

Код от MSDN .

Надеюсь, это поможет!

1 голос
/ 17 июня 2010

Вы не можете получить файлы cookie HTTPOnly из CookieContainer.

из MSDN

... Вы всегда должны создавать CookieContainer для отправкизапрос, если вы хотите, чтобы куки были возвращены в ответ.Это также верно для файлов cookie HTTPOnly, которые вы не можете получить.

...