Пример #1
0
 internal PassportIdentity(string token)
 {
     _userInited = true;
     try
     {
         if (string.IsNullOrEmpty(token))
         {
             throw new Exception();
         }
         byte[] bytes = PassportAuthentication.DecodeCookie(token);
         using (MemoryStream ms = new MemoryStream(bytes))
         {
             using (BinaryReader reader = new BinaryReader(ms))
             {
                 _isAuthenticated = reader.ReadByte() == 1;
                 _isAdmin         = reader.ReadByte() == 1;
                 _id           = reader.ReadInt64();
                 _adminId      = reader.ReadInt64();
                 _name         = string.Empty;
                 _roleId       = reader.ReadInt64();
                 _adminRoleId  = reader.ReadInt64();
                 _creationDate = (DateTime)Types.GetDefaultValue(TType <DateTime> .Type);
                 _lastIp       = IPAddress.Any.ToString();
                 _lastTime     = (DateTime)Types.GetDefaultValue(TType <DateTime> .Type);
                 _loginCount   = 0L;
                 _userData     = string.Empty;
                 _sysData      = string.Empty;
             }
         }
     }
     catch (Exception)
     {
         _isAuthenticated = false;
         _isAdmin         = false;
         _id           = 0L;
         _adminId      = 0L;
         _name         = string.Empty;
         _roleId       = 0L;
         _adminRoleId  = 0L;
         _creationDate = (DateTime)Types.GetDefaultValue(TType <DateTime> .Type);
         _lastIp       = IPAddress.Any.ToString();
         _lastTime     = (DateTime)Types.GetDefaultValue(TType <DateTime> .Type);
         _loginCount   = 0L;
         _userData     = string.Empty;
         _sysData      = string.Empty;
     }
 }
Пример #2
0
        private void EnsureUserInfo()
        {
            if (!_userInited)
            {
                try
                {
                    HttpContext context = HttpContext.Current;
                    HttpCookie  cookie  = context.Request.Cookies[PassportAuthentication.CookieName];
                    if (cookie == null)
                    {
                        throw new Exception();
                    }
                    string value = cookie.Value;
                    if (string.IsNullOrEmpty(value))
                    {
                        throw new Exception();
                    }
                    byte[] bytes = PassportAuthentication.DecodeCookie(value);
                    using (MemoryStream ms = new MemoryStream(bytes))
                    {
                        using (BinaryReader reader = new BinaryReader(ms))
                        {
                            _isAuthenticated = reader.ReadByte() == 1;
                            _isAdmin         = reader.ReadByte() == 1;
                            _id           = reader.ReadInt64();
                            _adminId      = reader.ReadInt64();
                            _name         = reader.ReadString();
                            _roleId       = reader.ReadInt64();
                            _adminRoleId  = reader.ReadInt64();
                            _creationDate = new DateTime(reader.ReadInt64());
                            int    len  = reader.ReadByte();
                            byte[] buff = reader.ReadBytes(len);
                            _lastIp     = (new IPAddress(buff)).ToString();
                            _lastTime   = new DateTime(reader.ReadInt64());
                            _loginCount = reader.ReadInt64();
                            _userData   = reader.ReadString();
                            _sysData    = reader.ReadString();
                            switch (PassportAuthentication.Level)
                            {
                            case PassportLevel.Normal:
                                if (!string.Equals(_sysData, Controller.GetClientIp(context)))
                                {
                                    throw new Exception();
                                }
                                break;

                            case PassportLevel.High:
                                if (!string.Equals(_sysData, CacheProvider.Current.Get <string>(new string[] { Utility.PassportCacheName, _isAdmin ? Utility.PassportAdminCacheName : Utility.PassportUserCacheName, _id.ToString() })))
                                {
                                    throw new Exception();
                                }
                                break;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    _isAuthenticated = false;
                    _isAdmin         = false;
                    _id           = 0L;
                    _adminId      = 0L;
                    _name         = string.Empty;
                    _roleId       = 0L;
                    _adminRoleId  = 0L;
                    _creationDate = (DateTime)Types.GetDefaultValue(TType <DateTime> .Type);
                    _lastIp       = IPAddress.Any.ToString();
                    _lastTime     = (DateTime)Types.GetDefaultValue(TType <DateTime> .Type);
                    _loginCount   = 0L;
                    _userData     = string.Empty;
                    _sysData      = string.Empty;
                }

                _userInited = true;
            }
        }