public override string ToString() { IPAddress ip; if (!IPAddress.TryParse(_lastIp, out ip)) { ip = IPAddress.Any; } byte[] buff = ip.GetAddressBytes(); using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(ms)) { writer.Write((byte)(_isAuthenticated ? 1 : 0)); writer.Write((byte)(_isAdmin ? 1 : 0)); writer.Write(_id); writer.Write(_adminId); writer.Write(_name); writer.Write(_roleId); writer.Write(_adminRoleId); writer.Write(_creationDate.Ticks); writer.Write((byte)buff.Length); writer.Write(buff); writer.Write(_lastTime.Ticks); writer.Write(_loginCount); writer.Write(_userData); writer.Write(_sysData); } return(PassportAuthentication.EncodeCookie(ms.ToArray())); } }
public string GetToken() { using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(ms)) { writer.Write((byte)(_isAuthenticated ? 1 : 0)); writer.Write((byte)(_isAdmin ? 1 : 0)); writer.Write(_id); writer.Write(_adminId); writer.Write(_roleId); writer.Write(_adminRoleId); } return(PassportAuthentication.EncodeCookie(ms.ToArray())); } }
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; } }
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; } }