我现在正在使用ASP.NET登录控件来验证用户登录到我的网站的身份。我想改用CAS来登录。在登录页面上,我做了如下操作:
protected void Page_Load(object sender, EventArgs e)
{
//redirect to login page if user isn't login to CAS
if (Request.QueryString["ticket"] == null)
{
string service = Request.Url.GetLeftPart(UriPartial.Path);
Response.Redirect("https://cas.hou.edu.vn/cas/login?service=" + service);
}
//if user logged in to CAS, send request to validate and get the response
else
{
string ticket = Request.QueryString["ticket"].ToString();
string service = Request.Url.GetLeftPart(UriPartial.Path);
var request = WebRequest.Create("https://cas.hou.edu.vn/cas/serviceValidate?service=" + service + "&ticket=" + ticket) as HttpWebRequest;
XmlDocument xmlDoc = new XmlDocument();
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
xmlDoc.Load(response.GetResponseStream());
}
XmlNodeList authFail = xmlDoc.GetElementsByTagName("cas:authenticationFailure");
if (authFail.Count > 0)
{
Debug.WriteLine(authFail[0].InnerText);
}
else
{
XmlNodeList authSuccess = xmlDoc.GetElementsByTagName("cas:authenticationSuccess");
//Username
string userId = authSuccess[0].FirstChild.InnerText;
}
}
成功验证到CAS Server后,我得到响应并具有用户名。您能告诉我如何自定义登录控件以使用该用户名登录。 谢谢!!