加密cookies

 
加密cookies

EnCookies()
...{
      UserIdentity user
= dal.CheckLogin(Request.Params[ " userName " ].ToString(), Request.Params[ " passWord " ].ToString());
        
if (user != null )
        ...{
            
int  saveCookiesDayNum = Convert.ToInt16(ConfigurationManager.AppSettings[ " saveCookiesDayNum " ]);
            
string  cookstr;
            FormsAuthenticationTicket ft
= new  FormsAuthenticationTicket( 1 , user.UserId.ToString(), DateTime.Now, DateTime.Now.AddDays(saveCookiesDayNum), Chb.Checked, user.UserName.ToString() + " , " + user.UserRole.ToString());

            cookstr
= FormsAuthentication.Encrypt(ft);
            HttpCookie hc
= new  HttpCookie(FormsAuthentication.FormsCookieName, cookstr);
            hc.Expires
= ft.Expiration;
            Response.Cookies.Add(hc);
}

获取用户身份获取用户身份
#region  获取用户身份
        
/**//**//**/ ///   <summary>
        
///  
        
///   </summary>
        
///   <returns></returns>
         public   static  UserIdentity GetUserIdentity()
        ......{
            UserIdentity userIdentity
= new  UserIdentity();
            
if (HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName] == null )
            ......{
                
return   null ;
            }
            
else
            ......{
                System.Web.Security.FormsIdentity userFormsIdentity
= (System.Web.Security.FormsIdentity)HttpContext.Current.User.Identity;
                FormsAuthenticationTicket ticket
= userFormsIdentity.Ticket;
                userIdentity.UserId
= Convert.ToInt32(ticket.Name);
                userIdentity.UserName
= ticket.UserData.Split( ' , ' )[ 0 ];
                userIdentity.UserRole
= Convert.ToInt32(ticket.UserData.Split( ' , ' )[ 1 ]);
                
return  userIdentity;
            }
        }

    
/**//**//**/ ///   <summary>
    
///  用户身份
    
///   </summary>
     public   class  UserIdentity
    ......{
        
public  UserIdentity()
        ......{
        }
        
private   int  _userid;
        
private   string  _username;
        
private   int  _userrole;
        
/**//**//**/ ///   <summary>
        
///  
        
///   </summary>
         public   int  UserId
        ......{
            
set
            ......{
                _userid
= value;
            }
            
get
            ......{
                
return  _userid;
            }
        }
        
/**//**//**/ ///   <summary>
        
///  
        
///   </summary>
         public   string  UserName
        ......{
            
set
            ......{
                _username
= value;
            }
            
get
            ......{
                
return  _username;
            }
        }
       
/**//**//**/ ///   <summary>
       
///  
       
///   </summary>
         public   int  UserRole
        ......{
            
set
            ......{
                _userrole
= value;
            }
            
get
            ......{
                
return  _userrole;
            }
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值