Common
{
    
    public class Member
    {
        
/// <summary>
        
/// lgoin_User
        
/// </summary>
        private const string USER_LOGIN = "lgoin_User";

        
/// <summary>
        
/// 用户登陆,保存登陆信息
        
/// </summary>
        
/// <param name="admin"></param>
        public static void SaveLoginUserInfo(string userName)
        {
            
try
            {
                
if (string.IsNullOrEmpty(userName)) return;

                Admin admin 
= Factory.Factory.GetAdminIDAL().GetAdminByUserName(userName);
                HttpContext.Current.Session.Add(USER_LOGIN, admin);
                SetCookies(USER_LOGIN, admin.UserName);
            }
            
catch
            {
                
throw new Exception("登录信息保存失败!");
            }
        }


        
/// <summary>
        
/// 用户注销,清空登陆信息
        
/// </summary>
        public static void SingOut()
        {
            
if (HttpContext.Current.Session[USER_LOGIN] != null)
                HttpContext.Current.Session.Remove(USER_LOGIN);
            DeleteCookie(USER_LOGIN);
        }

        
/// <summary>
        
/// 是否已登录
        
/// </summary>
        public static bool IsLogon
        {
            
get
            {
                
return User != null;
            }
        }

        
        
/// <summary>
        
/// 登录用户id
        
/// </summary>
        public static int UserId
        {
            
get
            {
                
if (User != null)
                    
return User.Id;
                
return -1;
            }

        }

        
/// <summary>
        
/// 登录用户账号
        
/// </summary>
        public static string UserName
        {
            
get
            {
                
if (User != null)
                    
return User.UserName;
                
return "";
            }
        }

        
/// <summary>
        
/// 登录用户类型
        
/// </summary>
        public static int UserType
        {
            
get
            {
                
if (User != null)
                    
return User.Level;
                
return 0;
            }
        }

        
/// <summary>
        
/// 登录用户基本信息
        
/// </summary>
        private static Admin User
        {
            
get
            {
                
if (HttpContext.Current.Session[USER_LOGIN] != null)
                    
return (Admin)HttpContext.Current.Session[USER_LOGIN];

                
if (CookiesIsEx(USER_LOGIN))
                {
                    
string username = (string)GetValueFromCookies(USER_LOGIN);
                    IDAL.IAdminDAL iadmin 
= Factory.Factory.GetAdminIDAL();
                    
return iadmin.GetAdminByUserName(username);
                }
                
return null;
            }
        }

        
/// <summary>
        
/// 添加cookie
        
/// </summary>
        
/// <param name="cookisName">cookie名称</param>
        
/// <param name="value"></param>
        private static void SetCookies(string cookisName, string value)
        {
            HttpCookie cookie 
= System.Web.HttpContext.Current.Request.Cookies[cookisName];
            Common.Encrypt(value);

            
if (cookie == null)
                cookie 
= new HttpCookie(cookisName);
            cookie.Path 
= "/";
            cookie.Expires 
= DateTime.Now.AddMinutes(20);
            cookie.Value 
= value;
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        
/// <summary>
        
/// 删除cookis
        
/// </summary>
        
/// <param name="cookisName">cookis名称</param>
        private static void DeleteCookie(string cookisName)
        {
            HttpCookie cookie 
= new HttpCookie(cookisName);
            cookie.Path 
=  "/";
            cookie.Expires 
= DateTime.Now.AddDays(-1);
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
        }

        
/// <summary>
        
/// 根据cookie名称获取值
        
/// </summary>
        
/// <param name="cookisName"></param>
        
/// <returns></returns>
        private static object GetValueFromCookies(string cookisName)
        {
            
return System.Web.HttpContext.Current.Request.Cookies[cookisName].Value;
        }

        
/// <summary>
        
/// 是否存在Cookies
        
/// </summary>
        
/// <param name="cookisName"></param>
        
/// <returns></returns>
        private static bool CookiesIsEx(string cookisName)
        {
            
if (System.Web.HttpContext.Current.Request.Cookies[cookisName] == null)
                
return false;
            
return !(string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.Cookies[cookisName].Value));
        }

    }
}

相关文章: