예제 #1
0
        public static string GetLoginToken(int loginId, DateTime expirationDate, bool oneTimeUse = true)
        {
            byte[] expirationBytes = BitConverter.GetBytes(expirationDate.Ticks);
            byte[] loginIdBytes    = BitConverter.GetBytes(loginId);

            var randomBytes = GetRandomBytes();

            byte[] oneTimeUseBytes = BitConverter.GetBytes(oneTimeUse);

            byte[] allTheBytes = new byte[loginIdBytes.Length + expirationBytes.Length + randomBytes.Length + sizeof(bool)];
            Buffer.BlockCopy(loginIdBytes, 0, allTheBytes, 0, loginIdBytes.Length);
            Buffer.BlockCopy(expirationBytes, 0, allTheBytes, loginIdBytes.Length, expirationBytes.Length);
            Buffer.BlockCopy(randomBytes, 0, allTheBytes, loginIdBytes.Length + expirationBytes.Length, randomBytes.Length);
            Buffer.BlockCopy(oneTimeUseBytes, 0, allTheBytes, loginIdBytes.Length + expirationBytes.Length + randomBytes.Length, oneTimeUseBytes.Length);

            byte[] encryptedBytes = BasicEncryption.EncryptBytesToBytes_Aes(allTheBytes);
            string encryptedData  = BasicEncryption.Base64EncodeUrlSafe(encryptedBytes);

            return(encryptedData);
        }
예제 #2
0
 public static TokenModel ParseToken(string token)
 {
     try
     {
         var  encryptedBytes = BasicEncryption.Base64DecodeUrlSafe(token);
         var  decryptedBytes = BasicEncryption.DecryptBytesFromBytes_Aes(encryptedBytes);
         int  loginId        = BitConverter.ToInt32(decryptedBytes, 0);
         long ticks          = BitConverter.ToInt64(decryptedBytes, sizeof(int));
         bool oneTimeUse     = BitConverter.ToBoolean(decryptedBytes, sizeof(int) + sizeof(long) + 16);
         return(new TokenModel
         {
             ObjectId = loginId,
             Ticks = ticks,
             IsOneTimeUse = oneTimeUse
         });
     }
     catch (Exception)
     {
         return(null);
     }
 }