172 lines
7.4 KiB
C#
172 lines
7.4 KiB
C#
// Services.UserLoginHelper
|
||
using System;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Web;
|
||
using System.Web.Security;
|
||
using Models;
|
||
using Models.ModelItems;
|
||
using Models.View;
|
||
using Services.Cache;
|
||
using Services.Extensions;
|
||
using Services.Manager;
|
||
using Services.Tool;
|
||
|
||
public class UserLoginHelper
|
||
{
|
||
private static string CookieName_User = (ConfigHelper.GetConfigString("DBName") + "_UserInfo").ToMD5();
|
||
|
||
private static string CookieName_Token = (ConfigHelper.GetConfigString("DBName") + "_TokenInfo").ToMD5();
|
||
|
||
/// <summary>
|
||
/// ....
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static View_UserInfo CurrentUser()
|
||
{
|
||
View_UserInfo user = HttpContext.Current.Session[CookieName_User] as View_UserInfo;
|
||
if (user != null && CacheHelp.cacheSysUserInfo.FirstOrDefault(a => a.Pwd == user.Pwd && a.Uid == user.Uid && a.IsValid >=2 ) != null)
|
||
{
|
||
Logs.WriteLog("userֵ" + user.Uid);
|
||
return user;
|
||
}
|
||
if (HttpContext.Current.Session["ras"] != null && HttpContext.Current.Request.Cookies[CookieName_User] != null && HttpContext.Current.Request.Cookies[CookieName_Token] != null)
|
||
{
|
||
RSA rSA = HttpContext.Current.Session["ras"] as RSA;
|
||
string keyword = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[CookieName_User].Value).Trim().ToLower();
|
||
Logs.WriteLog("keyword" + keyword);
|
||
string value = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[CookieName_Token].Value);
|
||
Logs.WriteLog("value"+value);
|
||
string pwd = (string.IsNullOrEmpty(rSA.DecodeOrNull(value)) ? "" : rSA.DecodeOrNull(value));
|
||
View_UserInfo view_UserInfo = CacheHelp.cacheSysUserInfo.First(a => a.Uid == keyword);
|
||
UserInfo usernew = new UserInfo() { Uid = keyword.Trim().ToLower(), Pwd = pwd, CreateTime = view_UserInfo.CreateTime };
|
||
usernew = usernew.ComputePasswordHash();
|
||
if (view_UserInfo == null || view_UserInfo.Pwd != usernew.Pwd || view_UserInfo.IsValid == 1)
|
||
{
|
||
return null;
|
||
}
|
||
else
|
||
{
|
||
Logs.WriteLog("view_UserInfo" + view_UserInfo);
|
||
return view_UserInfo;
|
||
}
|
||
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public static UserInfo CurrentUserAsync(HttpContext context)
|
||
{
|
||
//δʹ<CEB4><CAB9> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
UserInfo userInfo = context.Session[CookieName_User] as UserInfo;
|
||
if (userInfo != null)
|
||
{
|
||
return userInfo;
|
||
}
|
||
if (context.Request.Cookies[CookieName_User] != null && context.Request.Cookies[CookieName_Token] != null)
|
||
{
|
||
RSA rSA = HttpContext.Current.Session["ras"] as RSA;
|
||
string keyword = context.Request.Cookies[CookieName_User].Value;
|
||
string pwd = (string.IsNullOrEmpty(rSA.DecodeOrNull(context.Request.Cookies[CookieName_Token].Value)) ? "" : rSA.DecodeOrNull(context.Request.Cookies[CookieName_Token].Value));
|
||
var user = SqlSugarBase.Db.Queryable<UserInfo>().First(x => x.Uid == keyword.Trim());
|
||
UserInfo usernew = user.Clones();
|
||
usernew.Pwd = pwd;
|
||
usernew = usernew.ComputePasswordHash();
|
||
return user;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public static View_UserInfo GetUserLoginBy(string keyword, string password, int tYPE = 0)
|
||
{
|
||
string pwd = password;
|
||
if (tYPE == 0) {
|
||
RSA rSA = HttpContext.Current.Session["ras"] as RSA;
|
||
if (rSA == null)
|
||
{
|
||
throw new CustomException("<22><>Կ<EFBFBD><D4BF><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD>ˢ<EFBFBD>½<EFBFBD><C2BD><EFBFBD>!");
|
||
}
|
||
UserLogout();
|
||
if (string.IsNullOrEmpty(rSA.DecodeOrNull(password)))
|
||
{
|
||
throw new CustomException("<22><>Կ<EFBFBD><D4BF><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD>ˢ<EFBFBD>½<EFBFBD><C2BD><EFBFBD>!");
|
||
}
|
||
pwd = rSA.DecodeOrNull(password);
|
||
}
|
||
keyword = keyword.Trim().ToLower();
|
||
View_UserInfo view_UserInfo = CacheHelp.cacheSysUserInfo.FirstOrDefault( a => a.Uid.Trim().ToLower() == keyword);
|
||
if (view_UserInfo == null)
|
||
{
|
||
throw new CustomException("<22>˺<EFBFBD><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!");
|
||
}
|
||
UserInfo usernew = new UserInfo() { Uid = keyword.Trim(), Pwd = pwd, CreateTime = view_UserInfo == null ? DateTime.Now : view_UserInfo.CreateTime };
|
||
usernew = usernew.ComputePasswordHash();
|
||
|
||
string str = usernew.HashCode(view_UserInfo.Uid.ToUpper() + pwd +( view_UserInfo == null ? DateTime.Now : view_UserInfo.CreateTime).ToString("yyyy-MM-dd HH:mm:ss"));
|
||
if (view_UserInfo == null || usernew.Pwd != view_UserInfo.Pwd)
|
||
{
|
||
throw new CustomException("<22>˺<EFBFBD><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!");
|
||
}
|
||
|
||
|
||
if (view_UserInfo.IsValid == 1)
|
||
throw new CustomException("<22>˺Ŷ<CBBA><C5B6><EFBFBD>!");
|
||
else
|
||
{
|
||
if (view_UserInfo.IsValid < 2)
|
||
throw new CustomException("<22><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>!");
|
||
}
|
||
HttpContext.Current.Session[CookieName_User] = view_UserInfo;
|
||
CookieExtensions.WriteCookie(CookieName_User, HttpUtility.UrlEncode(keyword, Encoding.GetEncoding("UTF-8")), 60);
|
||
CookieExtensions.WriteCookie(CookieName_Token, HttpUtility.UrlEncode(password, Encoding.GetEncoding("UTF-8")), 60);
|
||
return view_UserInfo;
|
||
}
|
||
|
||
public static void UserLogout()
|
||
{
|
||
if (CheckUserLogin())
|
||
{
|
||
string name = HttpContext.Current.User.Identity.Name;
|
||
FormsAuthentication.SignOut();
|
||
RemoveUser(name);
|
||
}
|
||
}
|
||
public static void RemoveUser(string ID)
|
||
{
|
||
HttpContext.Current.Session.Clear();//ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Session
|
||
//HttpContext.Current.Response.Cookies.Clear();//ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Cookies
|
||
HttpCookie httpCookie = HttpContext.Current.Request.Cookies[CookieName_User];
|
||
httpCookie.Expires = DateTime.Now.AddDays(-1.0);
|
||
HttpContext.Current.Response.Cookies.Add(httpCookie);
|
||
HttpCookie httpCookie2 = HttpContext.Current.Request.Cookies[CookieName_Token];
|
||
httpCookie2.Expires = DateTime.Now.AddDays(-1.0);
|
||
HttpContext.Current.Response.Cookies.Add(httpCookie2);
|
||
}
|
||
|
||
public static bool CheckUserLogin()
|
||
{
|
||
try
|
||
{
|
||
if (HttpContext.Current.Request.Cookies[CookieName_User] != null && HttpContext.Current.Request.Cookies[CookieName_Token] != null)
|
||
{
|
||
string keyword = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[CookieName_User].Value);
|
||
RSA rSA = HttpContext.Current.Session["ras"] as RSA ?? throw new CustomException("ras<61><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>~");
|
||
string pwd = (string.IsNullOrEmpty(rSA.DecodeOrNull(HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[CookieName_Token].Value))) ? "" : rSA.DecodeOrNull(HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[CookieName_Token].Value)));
|
||
View_UserInfo view_UserInfo = CacheHelp.cacheSysUserInfo.Single((View_UserInfo a) => a.Uid == keyword);
|
||
UserInfo usernew = new UserInfo() { Uid = keyword.Trim(), Pwd = pwd, CreateTime = view_UserInfo == null ? DateTime.Now : view_UserInfo.CreateTime };
|
||
usernew = usernew.ComputePasswordHash();
|
||
if (view_UserInfo == null || view_UserInfo.Pwd != usernew.Pwd)
|
||
return false;
|
||
else
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Logs.WriteLog("<22><><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD>"+ ex.Message);
|
||
return false;
|
||
}
|
||
}
|
||
}
|