单点登陆 (一)

2014-11-23 23:40:09 · 作者: · 浏览: 47
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
{
    /// 
    /// 
    /// 
    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";
        /// 
        /// 
        /// 
        public const string filePassword = "Cn100cOM";
        const string defualtPassword = "Cn100.Co";
        const string appLoginName = "LoginPassWord";

        const string UserDataExpireMinutes = "UserDataExpireMinutes";


        /// 
        ///登陆过期时间
        /// 
        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;
            }
        }

        /// 
        /// 
        /// 
        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;
        }

        /// 
/// ///
/// 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.GetCookieva lueByName(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; } } /// /// 是否登陆 /// /// public static bool IsLogin() { CurrentUser user = new CurrentUser(); return user.isLogin(); //return