提示:“System.Net.ServicePointManager.CertificatePolicy”已过时,错误,求大神帮忙看看。
我要做网页的CAS单点登录的集成,按原文件编写如下,但是提示错误,求大神帮忙改正。代码如下:
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;
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.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)
{
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.Add("身份", 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 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