基于CAS在.NET中实现SSO单点登录
单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一。SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。
单点登录原理:存储信任、验证信任 。
CAS(Central Authentication Service )是 Yale 大学发起的一个企业级的、开源的项目,旨在为 Web 应用系统提供一种可靠的单点登录解决方法(属于 Web SSO)。本文针对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> <section name="casClientConfig" type="DotNetCasClient.Configuration.CasClientConfiguration, DotNetCasClient" /> configSections> configuration>
configuration/casClientConfig节点(需要修改)
<configuration> <casClientConfig casServerLoginUrl="http://localhost:8080/cas/login" casServerUrlPrefix="http://localhost:8080/cas/" serverName="http://localhost:8082/" notAuthorizedUrl="~/NotAuthorized.aspx" cookiesRequiredUrl="~/CookiesRequired.aspx" redirectAfterValidation="true" gateway="false" renew="false" singleSignOut="true" ticketTimeTolerance="5000" 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"> <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://blog.csdn.net/looserge/article/details/77987611 原文地址:转载请注明出处,谢谢!