单点登陆
using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.IO;
using System.Web.Security;
using System.Security.Cryptography;
using System.Web.Caching;
using Newtonsoft.Json;
using CN100.Member.IBLL.Modules;
using CN100.EnterprisePlatform.Wcf.Core;
using CN100.Member.IBLL;
using CN100.EnterprisePlatform.Wcf.Core.Config;
using CN100.ViewModel.Public;
using CN100.Member.Enums;
namespace CN100.Member.Permission
{
/// <summary>
///
/// </summary>
public class CurrentUser
{
private static string CookieName = "_Customer_Cn100_COM";
private static string MenuSessionName = "_SubAccountMenuUrls";
private static string LoginTimeCookie = "_Cn100_LoginTimeCookie";
const string cartCookieName = "_mCartCacheName";
const string messageCookieName = "_mMessageCookieName";
const string PassWordFile = "Key.ini";
const string cacheId = "cachePassword";
/// <summary>
///
/// </summary>
public const string filePassword = "Cn100cOM";
const string defualtPassword = "Cn100.Co";
const string appLoginName = "LoginPassWord";
const string UserDataExpireMinutes = "UserDataExpireMinutes";
/// <summary>
///登陆过期时间
/// </summary>
private static int ExpireMinutes
{
get
{
string strMin = System.Web.Configuration.WebConfigurationManager.AppSettings[UserDataExpireMinutes];
if (!string.IsNullOrEmpty(strMin))
{
int min = 0;
if (int.TryParse(strMin, out min))
{
return min;
}
}
return 30;
}
}
/// <summary>
///
/// </summary>
protected static string configPassword = GetRSAPassWord();
private static string GetRSAPassWord()
{
try
{
string strPwd = RSADecryption(System.Web.Configuration.WebConfigurationManager.AppSettings[appLoginName].ToString());
return strPwd;
}
catch (Exception ex)
{
Log.WriteLog(ex);
}
return "";
}
private static string GetPassWord()
{
string strPath = HttpContext.Current.Request.PhysicalApplicationPath + "/" + PassWordFile;
object objPassword = WebCache.GetFromWebCache(cacheId);
string strPassWord = "";
if (objPassword != null)
{
strPassWord = objPassword.ToString();
}
else
{
if (File.Exists(strPath))
{
FileStream stream = new FileStream(strPath, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(stream);
strPassWord = reader.ReadToEnd();
strPassWord = DecryptDES(strPassWord, filePassword);
stream.Close();
reader.Close();
//写入缓存
CacheDependency cacheDepen = new CacheDependency(strPath);
WebCache.WriteData(cacheId, cacheDepen, strPassWord);
}
else
{
FileStream stream = new FileStream(strPath, FileMode.CreateNew, FileAccess.Write);
strPassWord = EncryptDES(defualtPassword, filePassword);
byte[] btPass = Encoding.UTF8.GetBytes(strPassWord);
stream.Write(btPass, 0, btPass.Length);
stream.Close();
strPassWord = defualtPassword;
}
}
return strPassWord;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool isLogin()
{
lock (@Lock)
{
var cookie = CookieExt.GetCookie(CookieName);
if (cookie != null)
{
DateTime loginTime = GetLoginTime();
//延长cookie时间
// ExtendCookieTime(UserData);
if (loginTime.AddMinutes(ExpireMinutes) < DateTime.Now)
{
LogOut();
return false;
}
else
{
//延长登陆时间
ExtendCookieTime();
}
string UserData = CookieExt.GetCookieValueByName(CookieName);
if (!string.IsNullOrEmpty(UserData))
{
try
{
string decryptString = DecryptDES(UserData, configPassword);
var result = (LoginResult)JsonConvert.DeserializeObject(decryptString, typeof(LoginResult));
return true;
}
catch (Exception ex)
{
Log.WriteLog(ex);
}
}
}
return false;
}
}
/// <summary>
/// 是否登陆
/// </summary>
/// <returns></returns>
public static bool IsLogin()
{
CurrentUser user = new CurrentUser();
return user.isLogin();
//return HttpContext.Current.User.Identity.IsAuthenticated;
}
/// <summary>
/// 登出
/// </summary>
/// <returns></returns>
public static bool LogOut()
{
try
{
//System.Web.Security.FormsAuthentication.SignOut();
CookieExt.ClearCookie(CookieName);
CookieExt.ClearCookie(messageCookieName);
CookieExt.ClearCookie(cartCookieName);
//清除登陆时间
CookieExt.ClearCookie(LoginTimeCookie);
HttpContext.Current.Session.Remove(LoginTimeCookie);
//提现密码
CookieExt.ClearCookie("_CN100_DRACA_");
//提现错误次数
CookieExt.ClearCookie("_CN100_DRACOUNT_");
//移除子帐号菜单权限
if (HttpContext.Current.Session[MenuSessionName] != null)
{
HttpContext.Current.Session.Remove(MenuSessionName);
}
return true;
}
catch
{
return false;
}
}
/// <summary>
///
/// </summary>
public static object @Lock = new object();
/// <summary>
/// 获取当前用户信息
/// </summary>
public static LoginResult UserInfo
{
get
{
CurrentUser user = new CurrentUser();
return user.GetUserInfo();
}
}
/// <summary>
/// 获取用户信息
/// </summary>
/// <returns></returns>
public LoginResult GetUserInfo()
{
lock (@Lock)
{
LoginResult result = new LoginResult();
if (IsLogin())
{
string UserData = CookieExt.GetCookieValueByName(CookieName);
if (!string.IsNullOrEmpty(UserData))补充:软件开发 , C++ ,