2012-09-03 58 views
0

我有2個網站,第一個是myFirst.domain.com,第二個是mySecondSite.domain.comAuthenticationForm - 跨網站Cookie

他們留在兩個不同的Web服務器上,我的目標是允許跨站點身份驗證(我真正的需求是共享FormsAuthentication Cookie)。

我已經正確設置了我的web.config文件(機器關鍵節點,表單節點)。唯一的區別是關於loginUrl,在myFirstSite上的地址顯示爲~/login.aspx,在mySecondSite上顯示爲http://myFirstSite.com/login.aspx

請注意,我沒有虛擬目錄,我只有2個不同的網絡應用程序。

問題:當我從mySecondSite到達myFirstSite登錄頁面時,我從來沒有從登錄頁面重定向,它看起來像一個cookie沒有被寫入。

以下是關於這一問題的一些片段:

MyFirsSite:

<machineKey validationKey="..." decryptionKey="..." validation="SHA1" decryption="AES" /> 
     <authentication mode="Forms"> 
      <forms loginUrl="login.aspx" name="authCookie" enableCrossAppRedirects="true"></forms> 
     </authentication> 
     <authorization> 
      <deny users="?" /> 
      <allow users="*"/> 
     </authorization> 

MyFirstSite後面的代碼:

FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, "userName..", DateTime.Now, DateTime.Now.AddMinutes(30), true, "roles.."); 

     string ticket = FormsAuthentication.Encrypt(fat); 

     HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticket); 
     authCookie.Expires = fat.Expiration; 
     authCookie.Domain = "myDomain.com"; 
     Response.Cookies.Add(authCookie); 

//Here is other stuff about querystring checking in order to execute exact redirect, however it's not working, I always return to the login page. 

MySecondSite:

<machineKey validationKey="..." decryptionKey="..." validation="SHA1" decryption="AES"/> 
     <authentication mode="Forms"> 
      <forms loginUrl="http://myFirstSite.domain.com/login.aspx?queryStringToIndicateUrlPage" enableCrossAppRedirects="true"></forms> 
     </authentication> 
     <authorization> 

嗯,這是一個二。不幸的是它不起作用。

請不要關注queryStringToIndicateUrlPage,這只是一個簡單的解決方法,以便知道我是否必須重定向到相同的應用程序或另一個應用程序。

回答

0

當你使用兩個域之間的共享基於Cookie的身份驗證,則需要表明這一點在<forms>元素:「」

<machineKey validationKey="..." decryptionKey="..." validation="SHA1" decryption="AES"/> 
    <authentication mode="Forms"> 
     <forms domain=".domain.com" ... /> 
    </authentication> 

注意初始在域名之前,這使得在子域之間共享cookie成爲可能。

+0

我已經嘗試過指定域名,但它無法正常工作。 – bit

+0

@bit:您的示例代碼使用authCookie.Domain =「myDomain.com」,它應該是「.myDomain.com」。你嘗試過嗎? – Ruben

+0

是的,我做到了。我也嘗試了許多其他解決方案,但都沒有運氣。這似乎無法實現。 – bit