/// <summary>
/// 用户登录验证 返回用户ID,职位,权限信息 数组
/// </summary>
/// <param name="strUserName">用户名</param>
/// <param name="strPass">密码</param>
/// <param name="loginIp">登陆IP</param>
/// <returns>返回当前登陆用户的信息</returns>
public bool UserLogin(HttpContextBase context, string strUserName, string strPass)
{
string loginIP = context.Request.GetClientIP();
DataTable dt = TDal.UserLogin(strUserName, UsersBLL.GetPassWord(strPass), loginIP);
if (dt.Rows.Count < 1) return false;
RemoveModelCache(dt.Rows[0]["T_ServiceID"]);
Random rad = new Random();
int randCode = rad.Next(10001, 99999);
context.Response.Cookies["LoginInfo"].Value = DESEncrypt.Encrypt(randCode.ToString(), "LoginSafe");
HttpCookie hc = new HttpCookie("UserInfo");
hc.Values.Add("UID", DEncrypt.Encrypt(dt.Rows[0]["USID"].ToString(), randCode.ToString()));
hc.Values.Add("UCD", DEncrypt.Encrypt(dt.Rows[0]["USAccount"].ToString(), randCode.ToString()));
hc.Values.Add("RAD", DEncrypt.Encrypt(dt.Rows[0]["USRandomCode"].ToString(), randCode.ToString()));
hc.Values.Add("ULP", DEncrypt.Encrypt(loginIP, randCode.ToString()));
context.Response.Cookies.Add(hc);
context.Session["LoginTime"] = DateTime.Now;
IDictionary<string, object> dict = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
dict["UID"] = dt.Rows[0]["USID"].ToString().ToInt();
dict["UCD"] = dt.Rows[0]["USAccount"].ToString();
dict["UIP"] = loginIP;
dict["RAD"] = dt.Rows[0]["USRandomCode"].ToString().ToInt();
context.Items["UserLoginInfo"] = dict;
return true;
}
/// <summary>
/// 退出登陆
/// </summary>
/// <param name="context">Http请求信息</param>
public void LoginOut(HttpContextBase context)
{
int userid = GetLoginInfo(context).GetValue("UID", x => x.ToString().ToInt(), 0);
if (userid > 0) TDal.LoginOut(userid);
context.Request.Cookies.Clear();
context.Response.Cookies["LoginInfo"].Value = null;
context.Response.Cookies["UserInfo"].Value = null;
context.Session["LoginTime"] = null;
context.Items.Remove("UserLoginInfo");
}
private IDictionary<string, object> GetLoginInfo(HttpContextBase context)
{
string key = "UserLoginInfo";
IDictionary<string, object> dict = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
if (context.Items.Contains("UserLoginInfo"))
{
dict = context.Items[key] as IDictionary<string, object>;
return dict;
}
try
{
HttpCookie hc = context.Request.Cookies["LoginInfo"];
if (hc == null || String.IsNullOrEmpty(hc.Value)) return dict;
string randCode = DESEncrypt.Decrypt(hc.Value, "LoginSafe");
hc = context.Request.Cookies["UserInfo"];
if (hc == null || hc.Values.Count < 1 || String.IsNullOrEmpty(hc.Values["UID"])) return dict;
int uid = DEncrypt.Decrypt(hc.Values["UID"], randCode).ToInt();
string ucd = DEncrypt.Decrypt(hc.Values["UCD"], randCode);
string uip = DEncrypt.Decrypt(hc.Values["ULP"], randCode);
int rad = DEncrypt.Decrypt(hc.Values["RAD"], randCode).ToInt();
dict["UID"] = uid;
dict["UCD"] = ucd;
dict["UIP"] = uip;
dict["RAD"] = rad;
context.Items["UserLoginInfo"] = dict;
return dict;
}
catch { return dict; }
}
private IUserInfo GetLoginUser(HttpContextBase context)
{
string key = "LoginUserInfo";
IUserInfo user =null;
if (context.Items.Contains(key))
{
user = context.Items[key] as IUserInfo;
return user;
}
try
{
var dict = GetLoginInfo(context);
int uid = dict.GetValue("UID", x => x.ToString().ToInt(), 0);
user = GetModelByCache(uid,1);
context.Items[key] = user;
return user;
}
catch { return null; }
}
private bool IsLogin(HttpContextBase context, out string message)
{
message = "验证信息异常!";
try
{
var dict = GetLoginInfo(context);
int uid = dict.GetValue("UID", x => x.ToString().ToInt(), 0);
string ucd = dict.GetValue("UCD", "");
string uip = dict.GetValue("UIP", "");
int rad = dict.GetValue("RAD", x => x.ToString().ToInt(), 0);
DateTime checkDateTime = context.Session.Get<DateTime>("LoginTime", x => (DateTime)x, DateTime.MinValue);
bool isChecked = false;
if ((DateTime.Now - checkDateTime).TotalMinutes > 10)
{
isChecked = TDal.CheckUserLogin(uid, ucd, rad, context.Request.GetClientIP());
context.Session["LoginTime"] = DateTime.Now;
}
else
{
isChecked = uid > 0 && ucd != "" && uip == context.Request.GetClientIP();
}
if (!isChecked) message = "登陆信息失效!";
return isChecked;
}
catch { return false; }
}
#region 用户登陆权限信息接口
T IUserAuthorize.GetLoginUser<T>(System.Web.HttpContextBase context)
{
return (T)GetLoginUser(context);
}
IUserInfo IUserAuthorize.GetLoginUser(System.Web.HttpContextBase context)
{
return GetLoginUser(context);
}
T IUserAuthorize.GetUser<T>(int userId)
{
return (T)(object)this.GetModel(userId);
}
IUserInfo IUserAuthorize.GetUser(int userId)
{
return this.GetModel(userId);
}
System.Data.DataTable IUserAuthorize.GetUserList(string select, string where)
{
return this.GetDataList(select, where).Tables[0];
}
bool IUserAuthorize.IsAdministrator(System.Web.HttpContextBase context)
{
throw new NotImplementedException();
}
bool IUserAuthorize.IsHavePurview(System.Web.HttpContextBase context, string popValue)
{
throw new NotImplementedException();
}
bool IUserAuthorize.IsHavePurview(System.Web.HttpContextBase context, string modeCode, string action)
{
throw new NotImplementedException();
}
bool IUserAuthorize.IsHavePurviewByURL(System.Web.HttpContextBase context)
{
throw new NotImplementedException();
}
bool IUserAuthorize.IsHaveRole(System.Web.HttpContextBase context, int roleId)
{
throw new NotImplementedException();
}
bool IUserAuthorize.IsLogin(System.Web.HttpContextBase context, out string message)
{
return IsLogin(context, out message);
}
bool IUserAuthorize.IsLogin(System.Web.HttpContextBase context)
{
string message;
return IsLogin(context, out message);
}
#endregion