namespace Common {
public class PageManager:Admin.UserManager
{
/// 用户状态
public UserState AdminState { get; set; }
/// 用户名
protected string UserName { get; set; }
/// 密码
protected string UserPwd { get; set; }
/// 验证码
protected string CodeWord { get; set; }
/// 登录方式
public string PageRedirect { get { return PageUrl; } }
public string LoginInfo { get { return LoginState; } }
private object cache { get; set; }
/// 当前登录用户信息
public Object Cache { get { return cache; } }
application.CaCheUserData c = new application.CaCheUserData(true);
// pageManager的构造函数
public PageManager()
{
if (!IsPostBack)
{
this.Init += new EventHandler(PageManager_Init);
this.Load+=newEventHandler(PageManager_Load);
}
}
public PageManager(bool back)
{
}
public PageManager(string userName,string userPwd,string code)
{
UserName = userName;
UserPwd = userPwd;
CodeWord = code;
Manager();
}
private void Manager()
{
///缓存当前用户信息
object obj = GetState( SetPassWord(UserPwd),SetUserName(UserName), CodeWord);
if(LoginState==System.Configuration.ConfigurationManager.AppSettings.Get("onlineteach"))
{
Session["app_user"] = UserName;
Session["app_pageUrl"] = PageUrl;
AdminState = UserState.online;
///缓存在线人员
System.Web.HttpContext.Current.Application.Lock();
System.Web.HttpContext.Current.Application["TheUserLine"] += Session.SessionID+":"+UserType+"&"+UserName+"|";
System.Web.HttpContext.Current.Application.UnLock();
///将用户信息缓存应运程序中
c.cache_name = UserName;
c.CreateCache(obj);
System.Web.Security.FormsAuthentication.SetAuthCookie(UserName, false);
}
Else
{
AdminState = UserState.error;
}
}
protected virtual void PageManager_Load(object s, EventArgs e)
{
///页面载时操作
if (Cache == null)
GetUserData();
}
/// 获取用户的状态
private void GetState()
{
Try
{
if (AdminState != UserState.online || Session["app_user"] == null)
{
LoginOut();
AdminState = UserState.off;
}
}
Catch
{
AdminState = UserState.off;
}
Finally
{
if (UserState.off == AdminState)
{
System.Web.HttpContext.Current.Response.Redirect("/Application/login.aspx");
}
Else
AdminState = UserState.online;
}
}
public virtual void PageManager_Init(object sender, EventArgs e)
{
///判断用户状态
GetState();
//当前页面不会缓存
Response.Expires = 0;
Response.ExpiresAbsolute = DateTime.Now.AddMinutes(-1);
Response.AddHeader("pragma","no-cache");
Response.AddHeader("cache-control","private");
Response.CacheControl = "no-cache";
}
/// 用户状态
public enum UserState
{
online,
leave,
off,
Error
}
/// 获取登录号
public string GetUserName()
{
GetState();
if (AdminState == UserState.online)
{
return Session["app_user"].ToString();
}
Else
{
return null;
}
}
/// 获取?用户主页
public string GetPageUrl()
{
GetState();
if (AdminState == UserState.online)
{
return Session["app_pageUrl"].ToString();
}
Else
{
return null;
}
}
/// 获取用户信息
public void GetUserData()
{
this.cache = c.returnValue(GetUserName());
}
public string SetPassWord(string Pwd)
{
string c = System.Configuration.ConfigurationManager.AppSettings["pwdnumber"];
byte[] b = System.Text.Encoding.UTF8.GetBytes(c);
HMACMD5 hmacmd = new HMACMD5(b);
Pwd = hmacmd.GetMd5pwd(hmacmd, Pwd);
return Pwd;
}
public string SetUserName(string Name)
{
string d = System.Configuration.ConfigurationManager.AppSettings["usernumber"];
byte[] t = t = System.Text.Encoding.UTF8.GetBytes(d);
HMACMD5 hmacmd = new HMACMD5(t);
Name = hmacmd.GetMd5pwd(hmacmd, Name);
return Name;
}
public void LoginOut()
{
///在应用程序中清除该用户的信息
if (System.Web.HttpContext.Current.Application["TheUserLine"] != null)
{
string[] str = System.Web.HttpContext.Current.Application["TheUserLine"].ToString().Split('|');
string temp = "";
for (int i = 0; i < str.Length;i++ )
{
if (str[i].IndexOf(Session.SessionID) != -1)
{
temp = str[i];
break;
}
}
if (!string.IsNullOrEmpty(temp))
System.Web.HttpContext.Current.Application["TheUserLine"] =System.Web.HttpContext.Current.Application["TheUserLine"].ToString().Replace(temp+"|", "");
}
Session.Clear();
AdminState = UserState.off;
System.Web.Security.FormsAuthentication.SignOut();
System.Web.HttpContext.Current.Response.Redirect("/Application/login.aspx");
}
}
}