网上可以下载DotNetCasClient类库 以及ExampleWebSite运用实例 在VS 中加载即可测试
在VS中测试都很顺利,但发布到IIS上,会遇到定向循环问题,这个问题困扰了几天,测试了很多网上的解决办法,都没有彻底解决,不过在这个过程倒是对cas认证流程有了更深刻的理解。
1.在新建项目中,把编译的DotNetCasClient复制到项目bin目录
2. web.config中相关配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="casClientConfig" type="DotNetCasClient.Configuration.CasClientConfiguration, DotNetCasClient"/>
</configSections>
<connectionStrings>
...
</connectionStrings>
<casClientConfig
casServerLoginUrl="https://author.linbsoft.com:8443/cas/login"
casServerUrlPrefix="https://author.linbsoft.com:8443/cas"
serverName="http://net.linbsoft.com/casmytest"
notAuthorizedUrl="~/NotAuthorized.aspx"
cookiesRequiredUrl="~/CookiesRequired.aspx"
redirectAfterValidation="true"
gateway="false"
renew="false"
singleSignOut="true"
ticketTimeTolerance="5000"
ticketValidatorName="Cas20"
serviceTicketManager="CacheServiceTicketManager"
gatewayStatusCookieName="CasGatewayStatus" />
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms
loginUrl="https://author.linbsoft.com:8443/cas/login"
timeout="30"
defaultUrl="~/Default.aspx"
cookieless="UseCookies"
slidingExpiration="true"
path="/casmytest/" />
</authentication>
<authorization>
<deny users="?" />
<allow users="*"/>
</authorization>
...
<httpModules>
<add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient"/>
</httpModules>
<sessionState mode="StateServer" cookieless="UseCookies" timeout="36000"></sessionState>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="DotNetCasClient"/>
<add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient"/>
</modules>
</system.webServer>
<system.diagnostics/>
</configuration>
3. 出现“重定向过多的问题”,开始是网上的说法,
循环重定向到login页面的原因是每一次的验证都是失败,票据丢失,会话状态丢失。这个是引起循环重定向的直接凶手,
请求系统页面->httpModel重定向->cas登录页->登录后的系统页面->httpModel验证ticket失败->重定向登录cas
解决办法是增加:<sessionStatemode="StateServer"cookieless="UseCookies"timeout="36000"></sessionState>
但问题并没有彻底解决,最后通过文章《Yale CAS + .net Client 实现 SSO(3)--实现 ASP.NET WebForm Client》提出的方案解决问题,记录如下:
修改DotNetCasClient源码:
(1)在Visual Studio中打开“CASDotNetClient”项目中的“\Utils\HttpUtil.cs”文件,添加如下命名空间:
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
(2)在HttpUtil类中增加如下方法:
internal static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
(3)在PerformHttpGet方法中添加验证服务器证书回调自动验证代码,添加代码后的PerformHttpGet方法如下:
internal static string PerformHttpGet(string url, bool requireHttp200)
{
string responseBody = null;
//-- 以下新添加的代码:验证服务器证书回调自动验证
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
//-- 以上为新添加的代码
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
.(4)保存修改并重新生成解决方案。
这个方法测试可行,解决了循环重定向问题
5. 与自己的原有应用集成的方法(有个项目是VB.net的)
(1)复制DotNetCasClient.dll 到目标应用
(2)按上面的web.config 增加相应的拦截代码
(3)登录成功后,可以指定先跳转到如caslogin.aspx,主要是按应用登录的逻辑设置session等,然后再跳转到应用的主页,如:
'添加session 或访问数据库,查询用户登录信息
Session("UserName") = CasAuthentication.CurrentPrincipal.Identity.Name
Response.Redirect("~/Default.aspx")
(4)退出登录,先执行退出应用的逻辑,再退出cas认证系统
Dim ticketCookie As HttpCookie = Request.Cookies(FormsAuthentication.FormsCookieName)
If Not IsNothing(ticketCookie) Then
Dim ticket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(ticketCookie.Value)
If (Not IsNothing(CasAuthentication.ServiceTicketManager) And ticket.UserData.Length > 0) Then
Dim casTicket As CasAuthenticationTicket = CasAuthentication.ServiceTicketManager.GetTicket(ticket.UserData)
CasAuthentication.ServiceTicketManager.RevokeTicket(casTicket.ServiceTicket)
CasAuthentication.ClearAuthCookie()
Session.Clear()
Session.Abandon()
Request.Cookies.Remove(FormsAuthentication.FormsCookieName)
Page.Response.Cookies.Remove(FormsAuthentication.FormsCookieName)
CasAuthentication.SingleSignOut()
End If
End If