一,修改配置文件
<!--
<authentication mode="Windows" />
-->
<authentication mode="Forms" >
<forms name="casauth" loginUrl="default.aspx" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
其中default.aspx为登陆页面,还有一个页面是success.aspx是登陆成功的页面。
二。
default.aspx.cs源码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
using System.Xml;
using System.IO;
using System.Net;
using System.Reflection;
namespace sso
{
public partial class _Default : System.Web.UI.Page
{
private const string CASHOST = "https://jdeps:8444/cas/"; //cas服务器的网址
string username = "";
string password = "";
string ps1 = "";
string ps2 = "";
protected void Page_Load(object sender, EventArgs e)
{
System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();// 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();//cas的登录名
}
}
// 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
{
Session["UserName"] = netid;
Label1.Text = "Welcome " + netid;
string query1 = "select * from db where db='" + netid + "'";
MySqlConnection myConnection1 = new MySqlConnection("server=192.168.132.16;user id=root;password=admin;database=sso");
MySqlCommand myCommand1 = new MySqlCommand(query1, myConnection1);
myConnection1.Open();
myCommand1.ExecuteNonQuery();
MySqlDataReader myDataReader1 = myCommand1.ExecuteReader();
while (myDataReader1.Read() == true)
{
ps1 = myDataReader1["username"].ToString();
ps2 = myDataReader1["password"].ToString();
}
username = ps1;
password = ps2;
FormsAuthentication.RedirectFromLoginPage(netid, false); // set netid in ASP.NET blocks
abc();//完成自动登录
}
}
private void abc()
{
if (username == "" && password == "")
{
username = TextBox1.Text;
password = TextBox2.Text;
}
else
{
username = ps1;
password = ps2;
}
string query = "select * from login where username='" + username + "'";
MySqlConnection myConnection = new MySqlConnection("server=192.168.132.16;user id=root;password=admin;database=sso");
MySqlCommand myCommand = new MySqlCommand(query, myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
MySqlDataReader myDataReader = myCommand.ExecuteReader();
string ps = "";
while (myDataReader.Read() == true)
{
ps = myDataReader["password"].ToString();
}
myDataReader.Close();
myConnection.Close();
if (ps == password)
{
Response.Redirect("success.aspx");
}
else
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
abc();
}
}
}
三。新建一个类 代码如下
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
namespace sso
{
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
}
四。数据库
db username password//映射的关系表
username password//登录的用户名 密码
就是通过cas的登陆用户名找到username password 完成自动登录