编译器错误CS0535,验证证书过程出错,求大神帮忙。

mr_mrl 2016-04-01 09:16:25
编写的代码如下,请大神帮忙解决,十分感谢:

...全文
464 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mr_mrl 2016-04-02
  • 打赏
  • 举报
回复
然后就不知道怎么改了
mr_mrl 2016-04-02
  • 打赏
  • 举报
回复
这个是给的CAS集成帮助手册,我就是按照这个文件改的:全部代码如下: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Net; using System.Xml; using System.Net; using System.Security.Cryptography.X509Certificates; public partial class login : System.Web.UI.Page { // Local specific CAS host // CAS Server访问url,根据具体项目修改 private const string CASHOST = "http://cas.***.edu.cn/cas/"; protected void Page_Load(object sender, EventArgs e) { System.Net.ServicePointManager.CertificatePolicy = new MyPolicy(); // Look for the "ticket=" after the "?" in the URL string tkt = Request.QueryString["ticket"]; // This page is the CAS service=, but discard any query string residue string service = Request.Url.GetLeftPart(UriPartial.Path); // First time through there is no ticket=, so redirect to CAS login if (tkt == null || tkt.Length == 0) { string redir = CASHOST + "login?" + "service=" + service; Response.Redirect(redir); return; } // Second time (back from CAS) there is a ticket= to validate string validateurl = CASHOST + "serviceValidate?" + "ticket=" + tkt + "&"+ "service=" + service; StreamReader Reader = new StreamReader( new WebClient().OpenRead(validateurl)); string resp = Reader.ReadToEnd(); // I like to have the text in memory for debugging rather than parsing the stream // Some boilerplate to set up the parse. NameTable nt = new NameTable(); XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt); XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None); XmlTextReader reader = new XmlTextReader(resp, XmlNodeType.Element, context); string netid = null; // A very dumb use of XML. Just scan for the "user". If it isn't there, its an error. while (reader.Read()) { if (reader.IsStartElement()) { string tag = reader.LocalName; if (tag=="user") netid = reader.ReadString(); } } // if you want to parse the proxy chain, just add the logic above reader.Close(); // If there was a problem, leave the message on the screen. Otherwise, return to original page. if (netid == null) { //取得用户账号为空,按集成系统业务逻辑处理 } else { //netid即为用户账号,根据账号实现集成系统的登录 } } } public class MyPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint , X509Certificate certificate , WebRequest request , int certificateProblem) { //Return True to force the certificate to be accepted. return true; } // end CheckValidationResult } // class MyPolicy
Poopaye 2016-04-01
  • 打赏
  • 举报
回复
我的意思不是把你写的代码发到这来, 你从哪复制来(或者看来)的这个ICertificatePolicy, 这个接口只需要bool CheckValidationResult(ServicePoint, X509Certificate, WebRequest, int)方法
mr_mrl 2016-04-01
  • 打赏
  • 举报
回复
大神,按照你改的还是有错误,我只是个小白,什么也不懂。,请大神说得详细一点。谢谢了。
mr_mrl 2016-04-01
  • 打赏
  • 举报
回复
我错的,,全部代码如下: using System; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Web.UI.Adapters; using System.IO; using System.Xml; using System.Net; using System.Security.Cryptography.X509Certificates; using System.Net.Security; public partial class login : System.Web.UI.Page { // Local specific CAS host private const string CASHOST = "http://192.168.191.1:38080/cas/"; protected void Page_Load(object sender, EventArgs e) { System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(MyPolicy.RemoteCertificateValidationCallback); // Look for the "ticket=" after the "?" in the URL string tkt = Request.QueryString["ticket"]; // This page is the CAS service=, but discard any query string residue string service = Request.Url.GetLeftPart(UriPartial.Path); // First time through there is no ticket=, so redirect to CAS login if (tkt == null || tkt.Length == 0) { string redir = CASHOST + "login?" + "service=" + service; Response.Redirect(redir); return; } // Second time (back from CAS) there is a ticket= to validate string validateurl = CASHOST + "serviceValidate?" + "ticket=" + tkt + "&" + "service=" + service; StreamReader Reader = new StreamReader(new WebClient().OpenRead(validateurl)); string resp = Reader.ReadToEnd(); // I like to have the text in memory for debugging rather than parsing the stream // Some boilerplate to set up the parse. NameTable nt = new NameTable(); XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt); XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None); XmlTextReader reader = new XmlTextReader(resp, XmlNodeType.Element, context); string netid = null; // A very dumb use of XML. Just scan for the "user". If it isn't there, its an error. while (reader.Read()) { if (reader.IsStartElement()) { string tag = reader.LocalName; if (tag == "user") netid = reader.ReadString(); } } // if you want to parse the proxy chain, just add the logic above reader.Close(); // If there was a problem, leave the message on the screen. Otherwise, return to original page. if (netid == null) { Label1.Text = "CAS returned to this application, but then refused to validate your identity."; } else { if (!Page.IsPostBack) { SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DTDC"].ConnectionString); myConnection.Open(); string cmd_text = "SELECT * from 学生 where 标准学号='" + netid + "'"; string cmd_text01="SELECT * from 教师 where 标准工号='" + netid + "'"; SqlDataReader datar; SqlCommand mycommand = new SqlCommand(cmd_text, myConnection); SqlCommand mycommand01 = new SqlCommand(cmd_text01, myConnection); datar = mycommand.ExecuteReader(); while (datar.Read()) { if (datar != null) { Session["身份"] = "学生"; Response.Redirect ("~/index_student.aspx");} else { datar =mycommand01.ExecuteReader(); if (datar != null) { Session["身份"] = "教师"; Response.Redirect("~/index_teacher.aspx"); } else { Response.Redirect("~/index.aspx"); } } } myConnection.Close(); } Response.Write("连接成功"); } } } public class MyPolicy : ICertificatePolicy { public static bool RemoteCertificateValidationCallback( Object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors ) { //Return True to force the certificate to be accepted. return true; } } // class MyPolicy
Poopaye 2016-04-01
  • 打赏
  • 举报
回复
拷代码要拷全
  • 打赏
  • 举报
回复
直接这样写:ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧