單點登錄功能實現,用到了cas,關於cas的詳細介紹: https://apereo.github.io/cas/5.3.x/planning/Architecture.html
在項目使用時,服務端使用了基於JAVA CAS的服務端,
客戶端使用asp.net,在asp.net中使用非常簡單,github已有cas的.net 的 client ,鏈接:https://github.com/apereo/dotnet-cas-client
.net 端集成dotnetcasclient很簡單,按照官方的實例如下
(1)注冊casClientConfig Section
<configSections> <section name="casClientConfig" type="DotNetCasClient.Configuration.CasClientConfiguration, DotNetCasClient"/> <!-- Other custom sections here --> </configSections>
(2)新增 casClientConfig
<casClientConfig casServerLoginUrl="https://server.example.com/cas/login" casServerUrlPrefix="https://server.example.com/cas/" serverName="https://client.example.com:8443" notAuthorizedUrl="~/NotAuthorized.aspx" cookiesRequiredUrl="~/CookiesRequired.aspx" redirectAfterValidation="true" renew="false" singleSignOut="true" ticketValidatorName="Cas20" serviceTicketManager="CacheServiceTicketManager" />
(3)注冊CasAuthenticationModule
<system.web> <!-- Other system.web elements here --> <httpModules> <add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient"/> <!-- Other modules here --> </httpModules> </system.web>
(4)配置asp.net 的form登錄
<system.web> <authentication mode="Forms"> <forms loginUrl="https://server.example.com/cas/login" timeout="30" defaultUrl="~/Default.aspx" cookieless="UseCookies" slidingExpiration="true" path="/ApplicationName/" /> </authentication> <!-- Other system.web elements here --> </system.web>
以上的配置都可以在DotnetCasClient的github的README.md 看到,具體配置的解釋也是包含的
接下來就是發布網站到iis,就可以實現當前asp.net的站點的單點登錄
dotnetcasclient集成中的問題
在集成中遇到了幾個問題,這里做下備注
(1)cas服務端使用https,asp.net應用端使用了http,瀏覽網站出現循環重定向的錯誤
可以搜到很多類似問題的處理的文章,基本有三種種處理方法: 服務端和客戶端使用相互信任的證書,修改sessionstate 和 修改源碼。
參考了文章: https://blog.csdn.net/alonesword/article/details/9564787 后,最終選擇修改源碼
(2)asp.net應用端的單點登出功能異常
在查閱cas的基本原理后,確定是應用端的認證在服務端登出后沒有被銷毀,關閉瀏覽器會被銷毀,然而在實際應用中是不現實的,只能從服務端和客戶端的配置入手
1)確認服務端的配置,允許單點登出
2)確認了客戶端的配置,允許單點登出
官方signout的配置介紹: https://apereo.github.io/cas/development/installation/Logout-Single-Signout.html#single-logout-slo
然而,依然沒有解決
最終只能跟蹤dotnetclient的源碼,經過調試發現,服務端登出,本地應用沒有登出請求的通知。
最后發現由於是測試環境未分配正式域名,而測試和配置都是使用了域名進行驗證,幾台機器又未配置hosts導致。。。。
最終修改hosts的配置,將服務端和應用端的域名分別配置在hosts,問題得以解決