「C#入門」c#でhttponly cookieを取得

1.C#コード:

/// <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();
}
}

2.InternetGetCookieEx function関数について

関数説明

BOOL InternetGetCookieEx(
  _In_         LPCTSTR lpszURL,
  _In_         LPCTSTR lpszCookieName,
  _Inout_opt_  LPTSTR lpszCookieData,
  _Inout_      LPDWORD lpdwSize,
  _In_         DWORD dwFlags,
  _In_         LPVOID lpReserved
);

 

参考URL:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa384714%28v=vs.85%29.aspx

3.Remote Authentication in SharePoint Online Using the Client Object Model

参考URL:

http://code.msdn.microsoft.com/Remote-Authentication-in-b7b6f43c

Development

Posted by arkgame