ASP.NET MVC集成CAS客户端

最近公司的项目需要使用单点登录,用的是耶鲁大学的CAS单点登录,爬了一堆坑,把这次爬坑的经历分享出来,希望可以帮助到广大.NET的程序员。

首先去CAS官网上去下载.net版本的客户端代码。地址:https://apereo.github.io/cas/4.2.x/integration/CAS-Clients.html

官方已经为我们写好的客户端,我们不需要做任何的更改,下载完成之后把项目引入到我们的解决方案里面


引入项目之后我们接下来的工作就是更改Web项目的Web.config文件

在configSections节点下添加 <section name="casClientConfig" type="DotNetCasClient.Configuration.CasClientConfiguration,DotNetCasClient"/>

在configuration节点下添加:

<casClientConfig casServerLoginUrl="http://localhost:8080/cas/login"
                   casServerUrlPrefix="http://localhost:8080/cas/" serverName="http://localhost:2016"
                   notAuthorizedUrl="~/NotAuthorized.aspx" cookiesRequiredUrl="~/CookiesRequired.aspx"
                   redirectAfterValidation="true" gateway="false" renew="false" singleSignOut="true" ticketTimeTolerance="5000" ticketValidatorName="Cas20"  proxyTicketManager="CacheProxyTicketManager"
                   serviceTicketManager="CacheServiceTicketManager" gatewayStatusCookieName="casGatewayStatus"

/>

注:这里面casServerLoginUrl是cas服务端的地址,默认为https由于业务需要我这是http请大家注意;casServerUrlPrefix也是填cas服务端的地址,ServerName是客户端的地址剩下的不用动。

官方网站上有非常详细的配置内容,大家可以直接上官网查看如何配置,其实就是复制粘贴而已,官网都已经给配置好了,我们只需要更改服务端和客户端的地址

在system.web节点下添加 <authentication mode="Forms">
      <forms loginUrl="http://localhost:8080/cas/login" timeout="30" defaultUrl="~/Default.aspx" cookieless="UseCookies" slidingExpiration="true" path="/Account/Home"/>

    </authentication>

注:loginUrl填客户端的地址。path填验证完成之后跳回的MVC路由。

在system.web节点下httpModules节点中添加

 <add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient"/>

这个节点是加入客户端的HTTP管道

在system.webServer节点下添加 <validation validateIntegratedModeConfiguration="false"/>
    <modules>
      <!--
   Remove and Add the CasAuthenticationModule into the IIS7+
   Integrated Pipeline.  This has no effect on IIS5/6.
  -->
      <remove name="DotNetCasClient"/>
      <add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient"/>
      <!-- Other modules here -->
    </modules>

最后在configuration节点下添加

<system.diagnostics>
    <trace autoflush="true" useGlobalLock="false" />
  <sharedListeners>
    <!--
      Writing trace output to a log file is recommended.
      IMPORTANT:
      The user account under which the containing application pool runs
      must have privileges to create and modify the trace log file.
    -->
    <add name="TraceFile"
         type="System.Diagnostics.TextWriterTraceListener"
         initializeData="C:\inetpub\logs\LogFiles\DotNetCasClient.Log"
         traceOutputOptions="DateTime" />
  </sharedListeners>
  <sources>
    <!-- Provides diagnostic information on module configuration parameters. -->
    <source name="DotNetCasClient.Config" switchName="Config" switchType="System.Diagnostics.SourceSwitch" >
      <listeners>
        <add name="TraceFile" />
      </listeners>
    </source>
    <!-- Traces IHttpModule lifecycle events and meaningful operations performed therein. -->
    <source name="DotNetCasClient.HttpModule" switchName="HttpModule" switchType="System.Diagnostics.SourceSwitch" >
      <listeners>
        <add name="TraceFile" />
      </listeners>
    </source>
    <!-- Provides protocol message and routing information. -->
    <source name="DotNetCasClient.Protocol" switchName="Protocol" switchType="System.Diagnostics.SourceSwitch" >
      <listeners>
        <add name="TraceFile" />
      </listeners>
    </source>
    <!-- Provides details on security operations and notable security conditions. -->
    <source name="DotNetCasClient.Security" switchName="Security" switchType="System.Diagnostics.SourceSwitch" >
      <listeners>
        <add name="TraceFile" />
      </listeners>
    </source>
  </sources>
  <switches>
    <!--
      Set trace switches to appropriate logging level.  Recommended values in order of increasing verbosity:
       - Off
       - Error
       - Warning
       - Information
       - Verbose
    -->
    <!--
      Config category displays detailed information about CasAuthenticationModule configuration.
      The output of this category is only displayed when the module is initialized, which happens
      for the first request following application/server startup.
    -->
    <add name="Config" value="Information"/>
    <!--
      Set this category to Verbose to trace HttpModule lifecycle events in CasAuthenticationModule.
      This category produces voluminous output in Verbose mode and should be avoided except for
      limited periods of time troubleshooting vexing integration problems.
    -->
    <add name="HttpModule" value="Information"/>
    <!--
      Set to Verbose to display protocol messages between the client and server.
      This category is very helpful for troubleshooting integration problems.
    -->
    <add name="Protocol" value="Verbose"/>
    <!--
      Displays important security-related information.
    -->
    <add name="Security" value="Information"/>
  </switches>

</system.diagnostics>


到这里就配置完成了,当然官网其实写的很明了也很详细,大家如果看着乱可以上官网查看如何配置,在这里给大家贴出地址

https://github.com/apereo/dotnet-cas-client

配置完成之后在首页控制器的对应动作上添加[Authorize]特性


之后启动应用和CAS服务端你会发现应用会自动跳转到服务端的登录页面要求进行身份验证

至于为什么这样请参照考官网解释,毕竟官方的解释是最全面的

地址:https://wiki.jasig.org/pages/viewpage.action?pageId=32210981

应用启动后的页面


输入用户名和密码进行登录验证,这时经常会出现一个问题就是无限重定向,最后程序崩溃。

解决这个问题的方法:

在配置文件的system.web节点下添加

 <sessionState mode="StateServer"  cookieless="UseCookies" timeout="36000"></sessionState>

在客户端的代码中找到Utils文件夹下的HttpUtil类在PerformHttpGet方法中添加

 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);


然后在在计算器上右键:管理:服务

启动ASP.NET State Service服务,之后,无限重定向问题解决。

最后是获取用户登陆信息的问题

在控制器对应的Action 中添加以下代码

 var name = System.Web.HttpContext.Current.User.Identity.Name;  

name就是登陆所用的用户名,你就可以根据这个用户名去进行后续的操作了。

至此.NET集成CAS完毕,这个玩意简直遍地是雷,在学习过程中参考了以下文章,这些文章给予了我重大的提示

https://www.cnblogs.com/zhenyulu/archive/2013/01/22/2870936.html


http://www.cnblogs.com/xingshao/archive/2011/09/29/2195746.html


当然还要感谢一位在我学习过程中不断指导我的金大佬,接下来我可能会在最新版的.NET Core2.0版本中集成CAS如果集成成功我会继续把经验分享给大家,当然如果大家已经有实现了的欢迎指导我,如果大家发现文章中有错误内容请指正谢谢。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值