轻量级单点登录系统最佳实践(六)——5.1. 公共组件SSOLab.SSOServer.Components
Application.cs应用系统类。属性:ID、名称、单点登录秘钥。
ApplicationService.cs应用系统服务类。方法:根据名称获取应用系统。为了叙述简便,直接把应用系统的信息写入代码中。
User.cs用户类。属性:ID、用户名、密码。
UserService.cs用户服务类。方法:验证用户、根据ID获取用户、根据用户名获取用户。为了叙述简便,直接把用户的信息写入代码中。
SSOUtil.cs单点登录工具类。静态方法:获取随机字符串、DES加密、DES解密、获取网站地址
l Application.cs应用系统类。属性:ID、名称、单点登录秘钥。
l ApplicationService.cs应用系统服务类。方法:根据名称获取应用系统。为了叙述简便,直接把应用系统的信息写入代码中。
l User.cs用户类。属性:ID、用户名、密码。
l UserService.cs用户服务类。方法:验证用户、根据ID获取用户、根据用户名获取用户。为了叙述简便,直接把用户的信息写入代码中。
l SSOUtil.cs单点登录工具类。静态方法:获取随机字符串、DES加密、DES解密、获取网站地址
Application.cs
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace SSOLab.SSOServer.Components
{
public class Application
{
private string _id;
private string _name;
private string _ssoKey;
public string ID
{
get
{
return this._id;
}
set
{
this._id = value;
}
}
public string Name
{
get
{
return this._name;
}
set
{
this._name = value;
}
}
public string SSOKey
{
get
{
return this._ssoKey;
}
set
{
this._ssoKey = value;
}
}
public Application()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace SSOLab.SSOServer.Components
{
public class Application
{
private string _id;
private string _name;
private string _ssoKey;
public string ID
{
get
{
return this._id;
}
set
{
this._id = value;
}
}
public string Name
{
get
{
return this._name;
}
set
{
this._name = value;
}
}
public string SSOKey
{
get
{
return this._ssoKey;
}
set
{
this._ssoKey = value;
}
}
public Application()
{
}
}
}
ApplicationService
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace SSOLab.SSOServer.Components
{
public class ApplicationService
{
public static readonly int SSO_KEY_LENGTH = 128;
public Application GetApplicationByName(string name)
{
if (name == "portal")
{
Application application = new Application();
application.ID = "C8288957-B6AA-4522-99FF-7D1E60509974";
application.Name = "门户系统";
application.SSOKey = "Xj1wD4DT7UicRVxOBJdjAg2AjErHkoEDlB9GqMJtYMwbfbnc9slagStcVt0Y3lY0XVKDnn6nO9cnCPDwM0tJU6iCBlWEoomDfjAjhobLurOxHR8ua8a25NGNQXQ1Q34X";
return application;
}
else if (name == "app1")
{
Application application = new Application();
application.ID = "1466B140-D840-430a-9598-619D6888E9DB";
application.Name = "人力资源管理系统";
application.SSOKey = "XD0cEmXD0IcmYD0gBmYE0OdnYE1jHnZE1USnZF3y3GYpm93Gjp2s2GSog32FfoDm2FZoaG2FZnmhpkVBlJXkWB1eGkWCqA2lWC7k2lXCw1dlXDMqolZr805InrQk4Ixq";
return application;
}
else if (name == "app2")
{
Application application = new Application();
application.ID = "2A5F9A65-7490-480e-836F-DF18608D617B";
application.Name = "财务管理系统";
application.SSOKey = "XBtyndN8yHpZCiM1eO9XtE1qii9Oey17CYosH8cM7nRnXBIBjdN811pZrtw1PfhcBDyq7S9OeHcGmWAR7ycM7aloXBCsXQhe10FgrBEwPfSndDZpGwxbL55ymWAmhycM";
return application;
}
else if (name == "app3")
{
Application application = new Application();
application.ID = "F788FED3-32EE-470a-8DDF-0E6AD8A2FCEC";
application.Name = "网上办公系统";
application.SSOKey = "XJbbaaAnnQC67829OLkEKwgwiZL30oegpTbptQG0SLQG97665k4O32bb5CQdnffggufXJmBW16nZesssc2AOJl6bO0wiZLiu7k7FTbq27d0CdUG9110ykINvggh5CRjn";
return application;
}
else
{
return null;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace SSOLab.SSOServer.Components
{
public class ApplicationService
{
public static readonly int SSO_KEY_LENGTH = 128;
public Application GetApplicationByName(string name)
{
if (name == "portal")
{
Application application = new Application();
application.ID = "C8288957-B6AA-4522-99FF-7D1E60509974";
application.Name = "门户系统";
application.SSOKey = "Xj1wD4DT7UicRVxOBJdjAg2AjErHkoEDlB9GqMJtYMwbfbnc9slagStcVt0Y3lY0XVKDnn6nO9cnCPDwM0tJU6iCBlWEoomDfjAjhobLurOxHR8ua8a25NGNQXQ1Q34X";
return application;
}
else if (name == "app1")
{
Application application = new Application();
application.ID = "1466B140-D840-430a-9598-619D6888E9DB";
application.Name = "人力资源管理系统";
application.SSOKey = "XD0cEmXD0IcmYD0gBmYE0OdnYE1jHnZE1USnZF3y3GYpm93Gjp2s2GSog32FfoDm2FZoaG2FZnmhpkVBlJXkWB1eGkWCqA2lWC7k2lXCw1dlXDMqolZr805InrQk4Ixq";
return application;
}
else if (name == "app2")
{
Application application = new Application();
application.ID = "2A5F9A65-7490-480e-836F-DF18608D617B";
application.Name = "财务管理系统";
application.SSOKey = "XBtyndN8yHpZCiM1eO9XtE1qii9Oey17CYosH8cM7nRnXBIBjdN811pZrtw1PfhcBDyq7S9OeHcGmWAR7ycM7aloXBCsXQhe10FgrBEwPfSndDZpGwxbL55ymWAmhycM";
return application;
}
else if (name == "app3")
{
Application application = new Application();
application.ID = "F788FED3-32EE-470a-8DDF-0E6AD8A2FCEC";
application.Name = "网上办公系统";
application.SSOKey = "XJbbaaAnnQC67829OLkEKwgwiZL30oegpTbptQG0SLQG97665k4O32bb5CQdnffggufXJmBW16nZesssc2AOJl6bO0wiZLiu7k7FTbq27d0CdUG9110ykINvggh5CRjn";
return application;
}
else
{
return null;
}
}
}
}
User.cs
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace SSOLab.SSOServer.Components
{
public class User
{
private string _id;
private string _username;
private string _password;
public string ID
{
get
{
return this._id;
}
set
{
this._id = value;
}
}
public string Username
{
get
{
return this._username;
}
set
{
this._username = value;
}
}
public string Password
{
get
{
return this._password;
}
set
{
this._password = value;
}
}
public User()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace SSOLab.SSOServer.Components
{
public class User
{
private string _id;
private string _username;
private string _password;
public string ID
{
get
{
return this._id;
}
set
{
this._id = value;
}
}
public string Username
{
get
{
return this._username;
}
set
{
this._username = value;
}
}
public string Password
{
get
{
return this._password;
}
set
{
this._password = value;
}
}
public User()
{
}
}
}
UserService.cs
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace SSOLab.SSOServer.Components
{
public class UserService
{
public bool AuthenticationUser(string username, string password, out string id)
{
User user = GetUserByName(username);
if (user != null && user.Password == password)
{
id = user.ID;
return true;
}
else
{
id = String.Empty;
return false;
}
}
public User GetUserByID(string userID)
{
if (userID == "464FA65A-0DFF-46a9-AC0B-3EF1E4CDFF94")
{
User user = new User();
user.ID = "464FA65A-0DFF-46a9-AC0B-3EF1E4CDFF94";
user.Username = "admin";
user.Password = "admin";
return user;
}
else
{
return null;
}
}
public User GetUserByName(string username)
{
if (username == "admin")
{
User user = new User();
user.ID = "464FA65A-0DFF-46a9-AC0B-3EF1E4CDFF94";
user.Username = "admin";
user.Password = "admin";
return user;
}
else
{
return null;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace SSOLab.SSOServer.Components
{
public class UserService
{
public bool AuthenticationUser(string username, string password, out string id)
{
User user = GetUserByName(username);
if (user != null && user.Password == password)
{
id = user.ID;
return true;
}
else
{
id = String.Empty;
return false;
}
}
public User GetUserByID(string userID)
{
if (userID == "464FA65A-0DFF-46a9-AC0B-3EF1E4CDFF94")
{
User user = new User();
user.ID = "464FA65A-0DFF-46a9-AC0B-3EF1E4CDFF94";
user.Username = "admin";
user.Password = "admin";
return user;
}
else
{
return null;
}
}
public User GetUserByName(string username)
{
if (username == "admin")
{
User user = new User();
user.ID = "464FA65A-0DFF-46a9-AC0B-3EF1E4CDFF94";
user.Username = "admin";
user.Password = "admin";
return user;
}
else
{
return null;
}
}
}
SSOUtil.cs
Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace SSOLab.SSOServer.Components
{
public class SSOUtil
{
public static string GetRandomString(int length)
{
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++)
{
Random random = new Random(unchecked(i * (int)(DateTime.Now.Ticks)));
int ret = random.Next(122);
while (ret < 48 || (ret > 57 && ret < 65) || (ret > 90 && ret < 97))
{
ret = random.Next(122);
}
sb.Append((char)ret);
}
return sb.ToString();
}
public static string DESEncrypt(string text, string key)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = System.Security.Cryptography.CipherMode.ECB;
des.Padding = PaddingMode.Zeros;
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
byte[] inputBuffer = Encoding.GetEncoding("UTF-8").GetBytes(text);
byte[] outputBuffer = des.CreateEncryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
return Convert.ToBase64String(outputBuffer);
}
public static string DESDecrypt(string text, string key)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = System.Security.Cryptography.CipherMode.ECB;
des.Padding = PaddingMode.Zeros;
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
byte[] inputBuffer = Convert.FromBase64String(text);
byte[] outputBuffer = des.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
return Encoding.GetEncoding("UTF-8").GetString(outputBuffer);
}
public static string GetSiteUrl()
{
string path = HttpContext.Current.Request.ApplicationPath;
if (path.EndsWith("/") && path.Length == 1)
{
return GetHostUrl();
}
else
{
return GetHostUrl() + path;
}
}
public static string GetHostUrl()
{
return string.Format("{0}://{1}:{2}",
HttpContext.Current.Request.Url.Scheme,
HttpContext.Current.Request.Url.Host,
HttpContext.Current.Request.Url.Port);
}
}
}
using System;
using System.Collections.Generic;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace SSOLab.SSOServer.Components
{
public class SSOUtil
{
public static string GetRandomString(int length)
{
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++)
{
Random random = new Random(unchecked(i * (int)(DateTime.Now.Ticks)));
int ret = random.Next(122);
while (ret < 48 || (ret > 57 && ret < 65) || (ret > 90 && ret < 97))
{
ret = random.Next(122);
}
sb.Append((char)ret);
}
return sb.ToString();
}
public static string DESEncrypt(string text, string key)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = System.Security.Cryptography.CipherMode.ECB;
des.Padding = PaddingMode.Zeros;
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
byte[] inputBuffer = Encoding.GetEncoding("UTF-8").GetBytes(text);
byte[] outputBuffer = des.CreateEncryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
return Convert.ToBase64String(outputBuffer);
}
public static string DESDecrypt(string text, string key)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = System.Security.Cryptography.CipherMode.ECB;
des.Padding = PaddingMode.Zeros;
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
byte[] inputBuffer = Convert.FromBase64String(text);
byte[] outputBuffer = des.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
return Encoding.GetEncoding("UTF-8").GetString(outputBuffer);
}
public static string GetSiteUrl()
{
string path = HttpContext.Current.Request.ApplicationPath;
if (path.EndsWith("/") && path.Length == 1)
{
return GetHostUrl();
}
else
{
return GetHostUrl() + path;
}
}
public static string GetHostUrl()
{
return string.Format("{0}://{1}:{2}",
HttpContext.Current.Request.Url.Scheme,
HttpContext.Current.Request.Url.Host,
HttpContext.Current.Request.Url.Port);
}
}
}
posted on 2009-05-04 19:53 guushuuse 阅读(2100) 评论(5) 编辑 收藏 举报