所有代码如下:
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://202.118.40.14/cas/";
protected void Page_Load(object sender, EventArgs e)
{
ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
// 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)
{
Label.Text = "对不起,未能获取到您的账号,请重新登录。";
}
else
{
if (!Page.IsPostBack)
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DTDC"].ConnectionString);
myConnection.Open();
string cmd_text = "SELECT 学生id FROM 学生 WHERE 标准学号='" + netid + "'";
SqlCommand mycommand = new SqlCommand(cmd_text, myConnection);
SqlDataReader datar= mycommand.ExecuteReader();
datar.Read();
if (datar != null)
{
Session["身份"] = "学生";
Session["学号"] = netid;
datar.Close();
string sqlxm = "SELECT 姓名 FROM 学生 WHERE 标准学号='" + netid + "'";
string sqlzy = "SELECT 专业名称 FROM 学生 WHERE 标准学号='"+netid+ "'";
SqlCommand mycommandzy = new SqlCommand(sqlzy, myConnection);
SqlCommand mycommandxm = new SqlCommand(sqlxm, myConnection);
SqlDataReader zy = mycommandzy.ExecuteReader();
zy.Read();
Session["专业名称"] = zy;
Label.Text = "<b>专业名称:</b>";
Label.Text +=zy["专业名称"] + "" + "<br>";
zy.Close();
SqlDataReader xm = mycommandxm.ExecuteReader();
xm.Read();
Session["姓名"] = xm;
Label_session.Text += "(" + Session["学号"] + ")";
Label_session.Text += xm["姓名"] + "" ;
xm.Close();
string sqllw = "SELECT 论文题目 FROM 论文基本信息 WHERE 标准学号='" + netid + "'";
SqlCommand mycommandlw = new SqlCommand(sqllw, myConnection);
SqlDataReader lw = mycommandlw.ExecuteReader();
lw.Read();
Label.Text += "<b>论文题目:</b>";
Label.Text += lw["论文题目"] + "" + "<br>";
lw.Close();
string sqljs = "SELECT 指导教师 FROM 论文基本信息 WHERE 标准学号='" + netid + "'";
SqlCommand mycommandjs = new SqlCommand(sqljs, myConnection);
SqlDataReader js = mycommandjs.ExecuteReader();
js.Read();
Label.Text += "<b>指导教师:</b>";
Label.Text += js["指导教师"] + "";
js.Close();
Panel_userinfo_student.Visible = true;
Panel_userinfo_teacher.Visible = false;
//string sqlxy = "SELECT 学院名称 FROM 专业 WHERE 专业名称='" +zy+ "'";
//SqlCommand mycommandxy = new SqlCommand(sqlxy, myConnection);
//zy.Close();
// SqlDataReader xy = mycommandxy.ExecuteReader();
// xy.Read();
// Label.Text += xy["学院名称"] + "";
// Response.Write (netid);
//Response.Redirect("index_student.aspx");
}
else
{
string cmd_text01 = "SELECT 教师id FROM 教师 WHERE 标准工号='" + netid + "'";
SqlCommand mycommand01 = new SqlCommand(cmd_text01, myConnection);
SqlDataReader detar = mycommand01.ExecuteReader();
detar.Read();
if (detar != null)
{
Session["身份"] = "教师";
string sqlmz = "SELECT 姓名 FROM 教师 WHERE 标准工号='" + netid + "'";
string sqlzc = "SELECT 职称 FROM 教师 WHERE 标准工号='" + netid + "'";
SqlCommand mycommandmz = new SqlCommand(sqlmz, myConnection);
SqlCommand mycommandzc = new SqlCommand(sqlzc, myConnection);
SqlDataReader mz = mycommandmz.ExecuteReader();
mz.Read();
Label_session0.Text += mz["姓名"] + "";
Label_session0.Text += "(" + netid + ")";
mz.Close();
SqlDataReader zc = mycommandzc.ExecuteReader();
zc.Read();
Label_session0.Text += zc["职称"] + "";
zc.Close();
Panel_userinfo_student.Visible = false;
Panel_userinfo_teacher.Visible = true;
Response.Redirect("index_teacher.aspx");
}
else
{
Response.Redirect("index.aspx");
}
}
myConnection.Close();
}
}
}
public static bool RemoteCertificateValidate (
Object sender,
X509Certificate certificate,
X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors
)
{
//Return True to force the certificate to be accepted.
return true;
}
}
对于身份判断这个地方,对于学生能正常显示身份信息,但是用教师身份进行测试的时候,他还依然走学生应该走的程序,而不是直接跳到相应教师应该显示的信息语句。
这部分语句是身份判断的:判断是学生并显示相应的信息。

这部分是判断身份是教师不是学生,获得的datar应该为空,应该不走if(datar!=null)下面的语句,而应该直接走else下面的语句呀。

用学生账号测试能显示相应信息,但是用教师账号测试却提示一下错误:

求大神帮忙解答,十分感谢。