ASP.NETアプリケーション と 同じ仮想ディレクトリ内にある Webサービス(*.asmx等) 呼び出しで どうしてもセッションを共有したい場合、下記のようにすれば共有が可能になる。
Web サービスで ASP.NET セッション状態を使用する
http://msdn.microsoft.com/ja-jp/library/aa480509.aspx> HTTP サーバーの観点からは、 ASP.NET セッションのスコープは指定した ASP.NET アプリケーション内でのみ有効です。つまり、特定のユーザーの単一の仮想ディレクトリ内のすべてのセッション対応の ASP.NET 要求では、 HttpSessionState クラスの同じインスタンスが使用されます。(省略) ASP.NET はセッションに関する限り、ASPX 要求と ASMX 要求を区別しません。そのため、理論上は Web メソッド呼び出しと通常の ASPX ファイル間でセッション状態を共有できます。
① Webサービス
namespace ASPNETWebService
{
/// <summary>
/// EnableSession=true(セッションを利用する Webサービス)
/// </summary>
[WebService(Namespace = “http://tempuri.org/”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{[WebMethod(EnableSession = true)]
public string HelloWorld()
{
return String.Format(“Process={0}, Thread={1}, SessionID={2}”,
Process.GetCurrentProcess().Id,
Thread.CurrentThread.ManagedThreadId,
Session.SessionID);
}
}
}
② Webアプリケーション
namespace ASPNETWebApplication
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session[“Dummy”] = “1”;
}protected void ButtonCallWebService_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies[“ASP.NET_SessionId“];WSHelloWorld.Service1 service = new WSHelloWorld.Service1();
service.CookieContainer = new CookieContainer();
service.CookieContainer.Add(new Uri(service.Url), new System.Net.Cookie(cookie.Name, cookie.Value));string resultWebSearvice = service.HelloWorld();
Debug.WriteLine(“WebService=” + resultWebSearvice);
string resultWebApplication = String.Format(“Process={0}, Thread={1}, SessionID={2}”,
Process.GetCurrentProcess().Id,
Thread.CurrentThread.ManagedThreadId,
Session.SessionID);Debug.WriteLine(“WebApplication=” + resultWebApplication);
}
}
}
③ ActiveX
※ActiveXからクッキーを参照する場合
◎Win32 API
#define MAX_COOKIE_SIZE 4096
TCHAR szCookie[MAX_COOKIE_SIZE + 1];
DWORD dwSize = sizeof (szCookie) / sizeof (TCHAR);
if (InternetGetCookie(_T(“http://www.testsite.local/”), _T(“ASP.NET_SessionId“), szCookie, &dwSize))
{
TRACE(“Cookie=%s\n”, szCookie);
}
InternetGetCookie Function
http://msdn.microsoft.com/en-us/library/aa384710%28VS.85%29.aspx◎MFC
CString strCookieData;
if (CInternetSession::GetCookie(_T(“http://www.testsite.local/”), _T(“ASP.NET_SessionId“), strCookieData))
{
TRACE(“Cookie=%s\n”, (LPCTSTR)strCookieData);
}
CInternetSession::GetCookie
http://msdn.microsoft.com/ja-jp/library/cff9kt47.aspx