获取HttpOnly cookie。期待多重时只返回一个

时间:2015-07-07 09:26:13

标签: c# cookie-httponly

我找到了一个可以获得HttpOnly cookie的工作解决方案,但它只返回一个cookie,而我期待多个cookie。

有人可以告诉我我做错了吗?

    private const Int32 InternetCookieHttponly = 0x2000;
    [DllImport("wininet.dll", SetLastError = true)]
    public static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref uint pcchCookieData, int dwFlags, IntPtr lpReserved);
    const int INTERNET_COOKIE_HTTPONLY = 0x00002000;

    public static string GetGlobalCookies(string uri)
    {
        uint datasize = 1024;
        StringBuilder cookieData = new StringBuilder((int)datasize);
        if (InternetGetCookieEx(uri, "cookiename", cookieData, ref datasize, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero)
            && cookieData.Length > 0)
        {
            return cookieData.ToString().Replace(';', ',');
        }
        else
        {
            return null;
        }
    }

1 个答案:

答案 0 :(得分:0)

pchCookieName参数是要检索的cookie的区分大小写的名称。您正在传递字符串"cookiename",因此该函数将仅返回该cookie。

根据this MSDN code sample,您可以通过将null传递给此参数来检索所有Cookie。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 

namespace MSDN.Samples.ClaimsAuth 
{ 
    /// <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(); 
        } 
    } 
} 
相关问题