轻量级单点登录系统最佳实践(七)——5.2. 单点登录系统SSOLab.SSOServer.WebApp
SignIn.aspx单点登录系统登录页面。
SignIn.aspx.cs单点登录系统登录页面后台代码。用户登录成功后,返回应用系统相应页面。
SignOut.aspx单点登录系统注销页面。
SignOut.aspx.cs单点登录系统注销页面后台代码。
SSOContext.aspx单点登录系统上下文页面。
SSOContext.aspx.cs单点登录系统上下文页面后台代码。根据应用系统请求返回相应信息,其中用户信息为加密形式,每个应用系统采用不同的秘钥。
l SignIn.aspx单点登录系统登录页面。
l SignIn.aspx.cs单点登录系统登录页面后台代码。用户登录成功后,返回应用系统相应页面。
l SignOut.aspx单点登录系统注销页面。
l SignOut.aspx.cs单点登录系统注销页面后台代码。
l SSOContext.aspx单点登录系统上下文页面。
l SSOContext.aspx.cs单点登录系统上下文页面后台代码。根据应用系统请求返回相应信息,其中用户信息为加密形式,每个应用系统采用不同的秘钥。
SignIn.aspx
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SignIn.aspx.cs" Inherits="SSOLab.SSOServer.WebApp.SignIn" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="300">
<tr>
<td>
用户名
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server" />
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSignIn" runat="server" OnClick="btnSignIn_Click" Text="登录" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SignIn.aspx.cs" Inherits="SSOLab.SSOServer.WebApp.SignIn" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="300">
<tr>
<td>
用户名
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server" />
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSignIn" runat="server" OnClick="btnSignIn_Click" Text="登录" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
SignIn.aspx.cs
Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SSOLab.SSOServer.Components;
namespace SSOLab.SSOServer.WebApp
{
public partial class SignIn : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSignIn_Click(object sender, EventArgs e)
{
string userID;
bool passed = new UserService().AuthenticationUser(txtUsername.Text,
txtPassword.Text,
out userID);
if (passed && !String.IsNullOrEmpty(userID))
{
FormsAuthentication.SetAuthCookie(userID, true);
Session["USER_IS_LONGIN"] = true;
Session["USER_ID"] = userID;
string returnUrl = HttpUtility.UrlDecode(Request.Params["ReturnUrl"]);
Response.Redirect(returnUrl);
}
else
{
Session["USER_IS_LONGIN"] = false;
Session["USER_ID"] = String.Empty;
Response.Write("登录失败!");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SSOLab.SSOServer.Components;
namespace SSOLab.SSOServer.WebApp
{
public partial class SignIn : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSignIn_Click(object sender, EventArgs e)
{
string userID;
bool passed = new UserService().AuthenticationUser(txtUsername.Text,
txtPassword.Text,
out userID);
if (passed && !String.IsNullOrEmpty(userID))
{
FormsAuthentication.SetAuthCookie(userID, true);
Session["USER_IS_LONGIN"] = true;
Session["USER_ID"] = userID;
string returnUrl = HttpUtility.UrlDecode(Request.Params["ReturnUrl"]);
Response.Redirect(returnUrl);
}
else
{
Session["USER_IS_LONGIN"] = false;
Session["USER_ID"] = String.Empty;
Response.Write("登录失败!");
}
}
}
}
SignOut.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SignOut.aspx.cs" Inherits="SSOLab.SSOServer.WebApp.SignOut" %>
SignOut.aspx.cs
Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace SSOLab.SSOServer.WebApp
{
public partial class SignOut : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Session.Remove("USER_IS_LONGIN");
Session.Remove("USER_ID");
string returnUrl = HttpUtility.UrlDecode(Request.Params["ReturnUrl"]);
Response.Redirect(returnUrl);
}
}
}
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace SSOLab.SSOServer.WebApp
{
public partial class SignOut : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Session.Remove("USER_IS_LONGIN");
Session.Remove("USER_ID");
string returnUrl = HttpUtility.UrlDecode(Request.Params["ReturnUrl"]);
Response.Redirect(returnUrl);
}
}
}
SSOContext.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SSOContext.aspx.cs" Inherits="SSOLab.SSOServer.WebApp.SSOContext1" %>
SSOContext.aspx.cs
Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using SSOLab.SSOServer.Components;
namespace SSOLab.SSOServer.WebApp
{
public partial class SSOContext1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Response.ContentType = "text/javascript";
//Response.Buffer = true;
Response.Expires = 0;
Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
Response.AddHeader("pragma", "no-cache");
Response.CacheControl = "no-cache";
try
{
StringBuilder userInfo = new StringBuilder();
userInfo.Append("<userinfo>");
if (Session["USER_IS_LONGIN"] != null &&
(bool)Session["USER_IS_LONGIN"] == true &&
Session["USER_ID"] != null)
{
User user = new UserService().GetUserByID((string)Session["USER_ID"]);
if (user != null)
{
userInfo.Append("<id>").Append(user.ID).Append("</id>");
userInfo.Append("<username>").Append(user.Username).Append("</username>");
userInfo.Append("<islongin>true</islongin>");
}
else
{
userInfo.Append("<id></id>");
userInfo.Append("<username></username>");
userInfo.Append("<islongin>false</islongin>");
}
}
else
{
userInfo.Append("<id></id>");
userInfo.Append("<username></username>");
userInfo.Append("<islongin>false</islongin>");
}
userInfo.Append("<synchdate>").Append(DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss")).Append("</synchdate>");
userInfo.Append("</userinfo>");
StringBuilder sb = new StringBuilder();
sb.Append("function SSOContext(_signInUrl, _signOutUrl, _userInfo) {").Append("\r\n");
sb.Append("this.signInUrl=_signInUrl;").Append("\r\n");
sb.Append("this.signOutUrl=_signOutUrl;").Append("\r\n");
sb.Append("this.userInfo=_userInfo;").Append("\r\n");
sb.Append("}").Append("\r\n");
sb.Append("var ssoContext=");
sb.Append("new SSOContext(");
sb.Append("'").Append(SSOUtil.GetSiteUrl() + "/SignIn.aspx").Append("', ");
sb.Append("'").Append(SSOUtil.GetSiteUrl() + "/SignOut.aspx").Append("', ");
Application application = new ApplicationService().GetApplicationByName(Request.Params["app"]);
if (application != null &&
!String.IsNullOrEmpty(application.SSOKey) &&
application.SSOKey.Length >= 128)
{
sb.Append("'").Append(SSOUtil.DESEncrypt(userInfo.ToString(), application.SSOKey.Substring(application.SSOKey.Length / 2 - 1, 8))).Append("'");
}
else
{
sb.Append("' '");
}
sb.Append(");").Append("\r\n");
Response.Write(sb.ToString());
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using SSOLab.SSOServer.Components;
namespace SSOLab.SSOServer.WebApp
{
public partial class SSOContext1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Response.ContentType = "text/javascript";
//Response.Buffer = true;
Response.Expires = 0;
Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
Response.AddHeader("pragma", "no-cache");
Response.CacheControl = "no-cache";
try
{
StringBuilder userInfo = new StringBuilder();
userInfo.Append("<userinfo>");
if (Session["USER_IS_LONGIN"] != null &&
(bool)Session["USER_IS_LONGIN"] == true &&
Session["USER_ID"] != null)
{
User user = new UserService().GetUserByID((string)Session["USER_ID"]);
if (user != null)
{
userInfo.Append("<id>").Append(user.ID).Append("</id>");
userInfo.Append("<username>").Append(user.Username).Append("</username>");
userInfo.Append("<islongin>true</islongin>");
}
else
{
userInfo.Append("<id></id>");
userInfo.Append("<username></username>");
userInfo.Append("<islongin>false</islongin>");
}
}
else
{
userInfo.Append("<id></id>");
userInfo.Append("<username></username>");
userInfo.Append("<islongin>false</islongin>");
}
userInfo.Append("<synchdate>").Append(DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss")).Append("</synchdate>");
userInfo.Append("</userinfo>");
StringBuilder sb = new StringBuilder();
sb.Append("function SSOContext(_signInUrl, _signOutUrl, _userInfo) {").Append("\r\n");
sb.Append("this.signInUrl=_signInUrl;").Append("\r\n");
sb.Append("this.signOutUrl=_signOutUrl;").Append("\r\n");
sb.Append("this.userInfo=_userInfo;").Append("\r\n");
sb.Append("}").Append("\r\n");
sb.Append("var ssoContext=");
sb.Append("new SSOContext(");
sb.Append("'").Append(SSOUtil.GetSiteUrl() + "/SignIn.aspx").Append("', ");
sb.Append("'").Append(SSOUtil.GetSiteUrl() + "/SignOut.aspx").Append("', ");
Application application = new ApplicationService().GetApplicationByName(Request.Params["app"]);
if (application != null &&
!String.IsNullOrEmpty(application.SSOKey) &&
application.SSOKey.Length >= 128)
{
sb.Append("'").Append(SSOUtil.DESEncrypt(userInfo.ToString(), application.SSOKey.Substring(application.SSOKey.Length / 2 - 1, 8))).Append("'");
}
else
{
sb.Append("' '");
}
sb.Append(");").Append("\r\n");
Response.Write(sb.ToString());
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
Web.config
Code
<authentication mode="Forms">
<forms loginUrl="~/SignIn.aspx" name=".SSOServer" protection="All" slidingExpiration="true" timeout="60"></forms>
</authentication>
<authorization>
<allow users="*"/>
</authorization>
<authentication mode="Forms">
<forms loginUrl="~/SignIn.aspx" name=".SSOServer" protection="All" slidingExpiration="true" timeout="60"></forms>
</authentication>
<authorization>
<allow users="*"/>
</authorization>
posted on 2009-05-04 19:59 guushuuse 阅读(1995) 评论(2) 编辑 收藏 举报