CAS的配置过程分为服务端配置和客户端配置。
1.CAS服务端配置
CAS的服务端可以看作是为单点登陆提供的统一验证服务中心。 配置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登录会出现如下界面:
至此,说明服务端配置成功。 对于浏览器中必须使用HTTPS的提示,可以不用理会。如果有证书可以配置在tomcat下,但不要尝试使用keytool生成,利用keytool生成的证书,客户端在调用时无法保证有效的链接。2.CAS客户端搭建(在.NET项目中部署CAS)
- 下载.NET CAS client (如果不需要修改源码,则不需要下载源码,直接在NuGet控制台添加引用即可。)
https://pan.baidu.com/s/1kPbCCHKMrTHZr5OKtzF1Yw
- 在.NET项目中部署CAS
(1)创建一个ASP .NET MVC程序
(2)在NuGet控制台使用以下指令将DotNetCasClient添加到程序
PM> Install-Package DotNetCasClient也可以直接将DotNetCasClient源码附加到自己的解决方案中,方便调试。
(3)(很关键的步骤)配置web.config
以下配置都是在configuration节点中,如果是直接使用NuGet指令添加的DLL引用,则会在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]特性。[Authorize] public class HomeController : Controller { public ActionResult Index() { return View(); } }
(5)在VS中点击调试(可能要先将程序部署在IIS上),如果是未登陆的用户会自行跳转到CAS登陆界面。
(6)登陆成功之后,则会正常跳转。
(7)单点登出
修改原来的退出登录方法, 退出后回到CAS登录页面。[Authorize] public ActionResult LogOut() { FormsAuthentication.SignOut(); return Redirect("http://localhost:8080/cas/logout"); }
(8)至此asp.net MVC客户端配置已经完成,之后启动应用和CAS服务端你会发现应用自动跳转到服务端的登录页面要求进行身份验证。
参考:
https://www.cnblogs.com/woxpp/p/4653769.html https://blog.csdn.net/looserge/article/details/77987611 原文地址:https://www.cnblogs.com/imdeveloper/p/10513144.html转载请注明出处,谢谢!