我有一些HTTP模塊的驗證概念代碼。代碼檢查cookie是否存在,如果存在,它會檢索一個值,如果cookie不存在,它將創建它並設置該值。Sharepoint 2007中的HTTP模塊和Cookie
完成此操作後,我將寫入屏幕以查看已採取的操作(所有操作都很好且簡單)。所以在第一個請求中創建了cookie;隨後的請求從cookie中檢索值。
當我在一個正常的asp.net網站測試這一切正常工作 - 耶!然而,只要我將它轉移到SharePoint發生奇怪的事情,cookie就永遠不會被保存 - 也就是說,無論頁面刷新或次要請求如何,代碼始終會分支到創建cookie並從不讓分支檢索值。
繼承人的代碼...
public class SwithcMasterPage : IHttpModule
{
public void Dispose()
{
throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
// register handler
context.PreRequestHandlerExecute += new EventHandler(PreRequestHandlerExecute);
}
void PreRequestHandlerExecute(object sender, EventArgs e)
{
string outputText = string.Empty;
HttpCookie cookie = null;
string cookieName = "MPSetting";
cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie == null)
{
// cookie doesn't exist, create
HttpCookie ck = new HttpCookie(cookieName);
ck.Value = GetCorrectMasterPage();
ck.Expires = DateTime.Now.AddMinutes(5);
HttpContext.Current.Response.Cookies.Add(ck);
outputText = "storing master page setting in cookie.";
}
else
{
// get the master page from cookie
outputText = "retrieving master page setting from cookie.";
}
HttpContext.Current.Response.Write(outputText + "<br/>");
}
private string GetCorrectMasterPage()
{
// logic goes here to get the correct master page
return "/_catalogs/masterpage/BlackBand.master";
}