


1.CAS服務端配置
- Tomcat7.2 http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v8.5.38/bin/apache-tomcat-8.5.38-windows-x64.zip
- JDK
- CAS Service(cas-server-3.5.1-release) https://pan.baidu.com/s/1T31oL9PI2fy1AH9NBkt54Q
(1)解壓cas-server-3.5.1-release壓縮包,將解壓后的文件中modules文件夾中的cas-server-webapp-3.5.1.war文件拷貝到環境變量中配置的%TOMCAT_HOME%\webapps文件夾下,並修改文件名為:cas.war。
(2)配置完成后,啟動tomcat,瀏覽器輸入 https://127.0.01:8080/cas,會看到如下界面:
(3)輸入用戶名admin和密碼admin登錄會出現如下界面:
2.CAS客戶端搭建(在.NET項目中部署CAS)
- 下載.NET CAS client (如果不需要修改源碼,則不需要下載源碼,直接在NuGet控制台添加引用即可。)
- 在.NET項目中部署CAS
(1)創建一個ASP .NET MVC程序
(2)在NuGet控制台使用以下指令將DotNetCasClient添加到程序
PM> Install-Package DotNetCasClient

(3)(很關鍵的步驟)配置web.config
configuration/configSections節點(通過指令添加DLL時,此處無需修改)
<configuration> <configSections> <!--定義casClientConfig--> <section name="casClientConfig" type="DotNetCasClient.Configuration.CasClientConfiguration, DotNetCasClient" /> </configSections> </configuration>
configuration/casClientConfig節點(需要修改)
<configuration> <!--配置casClientConfig--> <casClientConfig <!--將casServerLoginUrl修改為自己配置的CAS登錄表單--> casServerLoginUrl="http://localhost:8080/cas/login" <!--將casServerUrlPrefix修改為自己配置的CAS服務器應用程序根--> casServerUrlPrefix="http://localhost:8080/cas/" <!--將serverName修改為自己配置的客戶端應用程序的地址(即自己的程序的默認首頁地址)--> serverName="http://localhost:8082/" notAuthorizedUrl="~/NotAuthorized.aspx" cookiesRequiredUrl="~/CookiesRequired.aspx" redirectAfterValidation="true" gateway="false" renew="false" <!--singlesignout:單點登出--> singleSignOut="true" ticketTimeTolerance="5000" <!--ticketvalidatorname:票驗證器驗證CAS票使用特定的協議名稱,有效值是cas10 cas20 saml11...--> ticketValidatorName="Cas20" proxyTicketManager="CacheProxyTicketManager" serviceTicketManager="CacheServiceTicketManager" gatewayStatusCookieName="CasGatewayStatus" /> </configuration>
configuration/system.web/authentication、configuration/system.web/httpModules節點(通過指令添加的DLL時,只需修改authentication節點)
<configuration> <system.web> <authentication mode="Forms"> <!--將loginUrl修改為自己配制的CAS登錄表單--> <forms name=".DotNetCasClientAuth" loginUrl="http://localhost:8080/cas/login" cookieless="UseCookies" /> </authentication> <httpModules> <add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient" /> </httpModules> </system.web> </configuration>
configuration/system.webServer/modules節點(通過指令添加DLL時,無需修改此處)
<configuration> <system.webServer> <系統注冊與ASP.NET管道casauthenticationmodule網絡部分表現在以下配置塊。--> <modules> <remove name="DotNetCasClient" /> <add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient" /> </modules> </system.webServer> </configuration>
(4)配置頁面權限驗證(很重要)
[Authorize] public class HomeController : Controller { public ActionResult Index() { return View(); } }
(5)在VS中點擊調試(可能要先將程序部署在IIS上),如果是未登陸的用戶會自行跳轉到CAS登陸界面。
(6)登陸成功之后,則會正常跳轉。
(7)單點登出
[Authorize] public ActionResult LogOut() { FormsAuthentication.SignOut(); return Redirect("http://localhost:8080/cas/logout"); }
(8)至此asp.net MVC客戶端配置已經完成,之后啟動應用和CAS服務端你會發現應用自動跳轉到服務端的登錄頁面要求進行身份驗證。
參考:
轉載請注明出處,謝謝!