2010-09-09 78 views
0

我有一些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"; 

    } 

回答

0

這原來是Web應用程序的身份驗證。要正常工作,您必須使用已配置爲表單身份驗證的FQDM。

0

您可以使用Fiddler或螢火蟲(上火狐)檢查響應,看是否正在發送您的Cookie。如果沒有,那麼也許你可以在PostRequestHandlerExecute中嘗試你的邏輯。這是假設Sharepoint或其他一些代碼正在修補響應cookie。這樣,你可以成爲添加cookie的最後一個。