static string SerializeToken(AuthToken entity) { using (HeavyProfiler.LogNoStackTrace("SerializeToken")) { var array = new MemoryStream().Using(ms => { using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress)) formatter.Serialize(ds, entity); return ms.ToArray(); }); array = Encrypt(array); return Convert.ToBase64String(array); } }
public static string RefreshToken(string oldToken, out UserEntity newUser) { AuthToken token = DeserializeToken(oldToken); newUser = AuthLogic.Disable().Using(_ => Database.Query<UserEntity>().SingleOrDefaultEx(u => u.Id == token.User.Id)); if (newUser == null) throw new AuthenticationException(AuthMessage.TheUserIsNotLongerInTheDatabase.NiceToString()); if (newUser.State == UserState.Disabled) throw new AuthenticationException(AuthMessage.User0IsDisabled.NiceToString(newUser)); if (newUser.UserName != token.User.UserName) throw new AuthenticationException(AuthMessage.InvalidUsername.NiceToString()); if (!newUser.PasswordHash.SequenceEqual(token.User.PasswordHash)) throw new AuthenticationException(AuthMessage.InvalidPassword.NiceToString()); AuthToken newToken = new AuthToken { User = newUser, CreationDate = TimeZoneManager.Now, }; var result = SerializeToken(newToken); return result; }
public static string CreateToken(UserEntity user) { AuthToken newToken = new AuthToken { User = user, CreationDate = TimeZoneManager.Now, }; return SerializeToken(newToken); }