码迷,mamicode.com
首页 > 其他好文 > 详细

获取HttpOnly的cookie

时间:2014-07-27 21:39:55      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>
/// 获取HttpOnly的cookie
/// 调用方式如下
/* String hostName = theBrowser.Url.Scheme + Uri.SchemeDelimiter + theBrowser.Url.Host;
Uri hostUri = new Uri(hostName);
string cookieStr = CookieHelper.GetCookieInternal(hostUri, false);
MatchCollection mc = Regex.Matches(cookieStr + ";", @"(.*?)=(.*?);");
foreach (Match mcookie in mc)
{
Cookie cklogin = new Cookie(mcookie.Groups[1].Value.Trim(), mcookie.Groups[2].Value);
cklogin.Domain = ".taobao.com";
cklogin.Path = "/";
WebCookie.Add(cklogin);
}
*/
/// </summary>
public class CookieHelper
{
[SecurityCritical]
public static string GetCookieInternal(Uri uri, bool throwIfNoCookie)
{
uint pchCookieData = 0;

string url = UriToString(uri);
uint flag = (uint)NativeMethods.InternetFlags.INTERNET_COOKIE_HTTPONLY;

if (NativeMethods.InternetGetCookieEx(url, null, null, ref pchCookieData, flag, IntPtr.Zero))
{
pchCookieData++;
StringBuilder cookieData = new StringBuilder((int)pchCookieData);

//Read the cookie
if (NativeMethods.InternetGetCookieEx(url, null, cookieData, ref pchCookieData, flag, IntPtr.Zero))
{
DemandWebPermission(uri);
return cookieData.ToString();
}
}

int lastErrorCode = Marshal.GetLastWin32Error();
if (throwIfNoCookie || (lastErrorCode != (int)NativeMethods.ErrorFlags.ERROR_NO_MORE_ITEMS))
{
throw new Win32Exception(lastErrorCode);
}
return null;
}

private static void DemandWebPermission(Uri uri)
{
string uriString = UriToString(uri);
if (uri.IsFile)
{
string localPath = uri.LocalPath;
new FileIOPermission(FileIOPermissionAccess.Read, localPath).Demand();
}
else
{
new WebPermission(NetworkAccess.Connect, uriString).Demand();
}
}

private static string UriToString(Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
UriComponents components = (uri.IsAbsoluteUri ? UriComponents.AbsoluteUri : UriComponents.SerializationInfoString);
return new StringBuilder(uri.GetComponents(components, UriFormat.SafeUnescaped), 2083).ToString();
}
}

 

 

internal sealed class NativeMethods
{
#region enums

 

public enum ErrorFlags
{
ERROR_INSUFFICIENT_BUFFER = 122,
ERROR_INVALID_PARAMETER = 87,
ERROR_NO_MORE_ITEMS = 259
}

 

public enum InternetFlags
{
INTERNET_COOKIE_HTTPONLY = 8192, //Requires IE 8 or higher
INTERNET_COOKIE_THIRD_PARTY = 131072,
INTERNET_FLAG_RESTRICTED_ZONE = 16
}

 

#endregion

 

#region DLL Imports

 

[SuppressUnmanagedCodeSecurity, SecurityCritical, DllImport("wininet.dll", EntryPoint = "InternetGetCookieExW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
internal static extern bool InternetGetCookieEx([In] string Url, [In] string cookieName, [Out] StringBuilder cookieData, [In, Out] ref uint pchCookieData, uint flags, IntPtr reserved);

 

#endregion
}

 

获取HttpOnly的cookie

标签:

原文地址:http://www.cnblogs.com/top15from/p/3871133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!