.net 集成CAS SSO单点登录、登出、重定向、全集

前提:脱机开发,页面使用asp.net,使用CAS SSO登陆系统,CAS Server 为企业现成的 ;

集成使用代码包:DotNetCasClient,下载地址:

https://download.csdn.net/download/soulman1234/11584023

1、将DotNetCasClient解压到本地:

2、生成DotNetCasClient

3、将这个dll引用到自己的登陆项目中去;

4、配置web.config

configuration第一个节点加上:

<configSections>
        <section name="casClientConfig" type="DotNetCasClient.Configuration.CasClientConfiguration, DotNetCasClient"/>
    </configSections>

继续加上,按自己的配置,改为自己的CASlogin、urlPrefix、ServerName,其他的默认即可:

<casClientConfig 
        casServerLoginUrl="https://cas.example.com/cas/login" 
        casServerUrlPrefix="https://cas.example.com/cas/" 
        serverName="cas.example.com" 
        notAuthorizedUrl="~/NotAuthorized.aspx" 
        cookiesRequiredUrl="~/CookiesRequired.aspx" 
        redirectAfterValidation="true" 
        gateway="false" 
        renew="false" 
        singleSignOut="true" 
        ticketTimeTolerance="5000" 
        ticketValidatorName="Cas20" 
        proxyTicketManager="CacheProxyTicketManager" 
        serviceTicketManager="CacheServiceTicketManager" 
        gatewayStatusCookieName="CasGatewayStatus" />

继续在system.web下加入,配置同样修改为自己的:

<authentication mode="Forms">
            <forms 
                loginUrl="https://cas.example.com/cas/login" 
                timeout="30" 
                defaultUrl="~/Default.aspx" 
                cookieless="UseCookies" 
                slidingExpiration="true" 
                path="/example/" />
        </authentication>
<authentication>
    <deny users="?" />
</authentication>
 <httpModules>
            <add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient"/>
        </httpModules>

继续在system.webServer下加入:

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

配置方面ok后,系统访问登陆页面将会自动跳转到SSO页面。

5、登陆页面

单点登出,这里通过点击登出按钮,写入一个cookie传参,接收到后根据cookie参数跳到Logout页面,主要的起作用的是:

FormsAuthentication.SignOut();

Response.Redirect();

为什么这么做呢?这里发现两个问题:

1、本想通过删除.DotNetCasClientAuth这个cookie,来完成注销,发现怎样都无法删除!

2、想通过注销后携带注销参数在url字符串后面来跳回登陆页面完成注销,注销虽然成功,但跳转到SSO登陆页面时,也会把这个参数带过去!

一直重定向跳转问题,可以参考吕震宇的博客,写的很详细:

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

同时,我还发现一个重定向问题,客户端如果部署到服务器上,服务器必须要可以访问到SSO服务端的域名。不然会出现一个问题

在本地访问SSO,,这时候如果本地改了host,可以访问到SSO服务端部署的域名,是可以跳转到SSO登陆地址的,然后输入账号、密码,就一直重定向错误了。这其实是你的服务端拿到tickets后,无法访问ServiceValidate地址验证票据。要确保客户端服务器host配置了SSO服务端的域名解析!

发布了22 篇原创文章 · 获赞 1 · 访问量 6918

猜你喜欢

转载自blog.csdn.net/soulman1234/article/details/99853639