ASP.NET2.0:FormsAuthenticationAcrossdomains

來源:互聯網
上載者:User

Keywords: single sign-on, across applications, across domains.

目標:實現對www.domain.com, sub1.domain.com, sub2.domain.com......的統一許可權認證,既single sign-on 單點登入。說明一點,這裡的每個地址對應的是一個獨立的應用,也就是說在IIS裡它們都是一個實際的網站。這和運用URL Rewiting技術實現的次層網域訪問是不同的。URL重寫實現的次層網域訪問,對應的伺服器端其實只是一個應用。實際上,IIS在處理這種次層網域的請求時,首先會檢查有沒有實際的網站配置在IIS上,如果有就會轉向這個這個網站(這裡討論的就是這種情況下的跨域認證問題),如果沒有就會轉向主機頭為空白的那個網站,例如訪問sub1.domain.com,如果IIS上沒有配置這個網域名稱,則iis就會把它轉向domain.com上,而URL Writing是在domain.com這個應用上進行的。扯遠了,還是回到single sign-on上。

實現:跨域的身份認證換句話說就是實現對認證資訊的共用,ASP.NET的Forms認證,認證資訊預設是加密儲存在cookie裡的,所以只要對cookie的加密方式,密鑰,儲存位置進行統一,認證資訊就可以共用了。ASP.NET 2.0中的實現其實非常簡單,只要修改一些配置資訊就可以了。以sub1.domain.com, sub2.domain.com兩個應用為例,為了實現兩者的統一認證,必須保證下列配置的一致(標註紅色的部分):

     <authentication mode="Forms">
      <forms name=".ASPXAUTH" loginUrl="signin.aspx" defaultUrl="~/default.aspx" protection="All" timeout="30" path="/" requireSSL="false" slidingExpiration="true" cookieless="UseDeviceProfile" domain="domain.com" enableCrossAppRedirects="true"/>
    </authentication>
    <machineKey validationKey='64E5DACAC6FB2B3E34FE06887639909EF2A180D6CDCB2463EAF098BA3E24A4FAC6C2A6E48C2F80FEE32A751C4B33D3EC70D73C59D64340E4CF7D9E44B0BA0077'   decryptionKey='BCCC63AF8E95BEA7393377CF2CF0CA35F1B5C09343404F44'   validation='SHA1'/>

另外,如果打算對角色資訊也可以共用,例如在sub1.domain.com裡認證為具有administrators角色的使用者在進入sub2.domain.com後仍然是administrators,則要保證以下配置資訊的一致:

    <roleManager cacheRolesInCookie="true" cookieName=".ASPXROLES" cookiePath="/" cookieProtection="All" cookieRequireSSL="false" domain="domain.com" cookieSlidingExpiration="true" cookieTimeout="30" createPersistentCookie="false" defaultProvider="SQLRoleProvider" enabled="true">
      <providers>
        <add name="SQLRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="MyConnectionString" applicationName="MyApp"/>
      </providers>
    </roleManager>

進而,如果想對認證資訊統一進行管理,(如membersihp,roles),則應該對membersihp, rolemanager配置節的實體儲存體配置進行統一,主要是資料庫的連結配置到統一的位置,applicationName配置成相同的。這時我們就會感覺到,ASP.NET 2.0 build-in的membersip, rolemanagement service,為我們實現集中的許可權認證提供了便利。由於實體儲存體的結構相同,整合就變得十分方便。

附:下面這個連結可以用來產生machine key配置:
http://www.eggheadcafe.com/articles/GenerateMachineKey/GenerateMachineKey.aspx
References:

MSDN Configuring Forms Authentication Across Applications
Creating a Single Sign-on for ASP.NET Application and Legacy ASP Application (Part II)
Single sign-on across multiple applications in ASP.NET
Single Sign-On for everyone - ASP.NET Forums

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.