客户端保存用户登陆信息

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Text;
using Model;
using System.Web;
using IDAL;

namespace 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));
        }

    }
}

转载于:https://www.cnblogs.com/di305449473/archive/2009/01/14/1375337.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值