(转)CAS 单点登录安装笔记4

  asp.net client端的设置

1、首先修改web.Config文件,加入以下设置:
Xml代码  
  1. <authentication mode="Forms" >  
  2.   <forms name="casauth" loginUrl="login.aspx" />  
  3. </authentication>  
  4. <authorization>  
  5.   <deny users="?" />  
  6. </authorization>  

本人对.net不是很熟悉,感觉这里的配置类似java web应用程序中的过滤器,当用户访问web页时首先跳转到login.aspx页面进行验证。

2、加入以下c#代码到login.aspx页面的加载事件中:
C#代码  
  1. //CAS 身份验证 服务器地址   
  2. private const string CASHOST = "https://sso.gzps.net:8443/cas/";   
  3.   
  4. protected void Page_Load(object sender, EventArgs e)   
  5. {   
  6.     System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();   
  7.   
  8.     // Look for the "ticket=" after the "?" in the URL   
  9.       string tkt = Request.QueryString["ticket"];   
  10.   
  11.       // This page is the CAS service=, but discard any query string residue   
  12.       string service = Request.Url.GetLeftPart(UriPartial.Path);   
  13.   
  14.       // First time through there is no ticket=, so redirect to CAS login   
  15.       if (tkt == null || tkt.Length == 0)   
  16.       {   
  17.         string redir = CASHOST + "login?" +   
  18.           "service=" + service;   
  19.         Response.Redirect(redir);   
  20.         return;   
  21.       }   
  22.   
  23.       // Second time (back from CAS) there is a ticket= to validate   
  24.       string validateurl = CASHOST + "serviceValidate?" +   
  25.         "ticket=" + tkt + "&"+   
  26.         "service=" + service;   
  27.       StreamReader Reader = new StreamReader( new WebClient().OpenRead(validateurl));   
  28.       string resp = Reader.ReadToEnd();   
  29.       // I like to have the text in memory for debugging rather than parsing the stream   
  30.   
  31.       // Some boilerplate to set up the parse.   
  32.       NameTable nt = new NameTable();   
  33.       XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);   
  34.       XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);   
  35.       XmlTextReader reader = new XmlTextReader(resp, XmlNodeType.Element, context);   
  36.   
  37.       string netid = null;   
  38.   
  39.       // A very dumb use of XML. Just scan for the "user". If it isn't there, its an error.   
  40.       while (reader.Read())   
  41.       {   
  42.         if (reader.IsStartElement()) {   
  43.           string tag = reader.LocalName;   
  44.           if (tag=="user")   
  45.             netid = reader.ReadString();   
  46.         }   
  47.       }   
  48.       // if you want to parse the proxy chain, just add the logic above   
  49.       reader.Close();   
  50.       // If there was a problem, leave the message on the screen. Otherwise, return to original page.   
  51.       if (netid == null)   
  52.       {   
  53.         Label1.Text = "CAS returned to this application, but then refused to validate your identity.";   
  54.       }   
  55.       else  
  56.       {   
  57.           Session["UserName"] = netid;   
  58.         Label1.Text = "Welcome " + netid;   
  59.         FormsAuthentication.RedirectFromLoginPage(netid, false); // set netid in ASP.NET blocks   
  60.       }   
  61.   
  62. }   


以上代码参照了ja-sig网站的解决方案:http://www.ja-sig.org/wiki/display/CASC/ASP.NET+Forms+Authentication

3、以为这样就可以了,运行时可以跳到sso服务器进行验证,但跳转以后报以下错误:
" System.Net.WebException。 基础连接已关闭。 无法建立与远程服务器信任关系 "。
应该与CAS Server端安装了数字证书,而.net Client端并没有安装相应的证书有关。
可以通过配置IIS服务器,支持HTTPS SSL协议实现安全数据交换中介绍的步骤导入CAS 服务端的数字证书,或者通过http://support.microsoft.com/kb/823177/上介绍的解决方案进行处理:
实现类
C#代码  
  1. using System.Net;   
  2. using System.Security.Cryptography.X509Certificates;   
  3.   
  4. public class MyPolicy : ICertificatePolicy {   
  5.     public bool CheckValidationResult(   
  6.           ServicePoint srvPoint   
  7.         , X509Certificate certificate   
  8.         , WebRequest request   
  9.         , int certificateProblem) {   
  10.   
  11.         //Return True to force the certificate to be accepted.   
  12.         return true;   
  13.   
  14.     } // end CheckValidationResult   
  15. // class MyPolicy  

客户端代码中包含下列代码:
#c代码  
  1. System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();  


所有代码见附件WebSite.rar,将其部署到你的IIS服务器就可以了。
关于IIS服务器的设置见asp.net一夜速成教程
  • WebSite.rar (4 KB)
  • 描述: asp.net 与cas结合的实例程序
原文地址:https://www.cnblogs.com/qq4004229/p/2334368.html