public static byte[] Decode(string encodedData, MachineKeyProtection protectionOption)
 {
     if (encodedData == null)
     {
         throw new ArgumentNullException("encodedData");
     }
     if ((encodedData.Length % 2) != 0)
     {
         throw new ArgumentException(null, "encodedData");
     }
     byte[] buf = null;
     try
     {
         buf = MachineKeySection.HexStringToByteArray(encodedData);
     }
     catch
     {
         throw new ArgumentException(null, "encodedData");
     }
     if ((buf == null) || (buf.Length < 1))
     {
         throw new ArgumentException(null, "encodedData");
     }
     if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Encryption))
     {
         buf = MachineKeySection.EncryptOrDecryptData(false, buf, null, 0, buf.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption);
         if (buf == null)
         {
             return(null);
         }
     }
     if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Validation))
     {
         if (buf.Length < MachineKeySection.HashSize)
         {
             return(null);
         }
         byte[] src = buf;
         buf = new byte[src.Length - MachineKeySection.HashSize];
         Buffer.BlockCopy(src, 0, buf, 0, buf.Length);
         byte[] buffer3 = MachineKeySection.HashData(buf, null, 0, buf.Length);
         if ((buffer3 == null) || (buffer3.Length != MachineKeySection.HashSize))
         {
             return(null);
         }
         for (int i = 0; i < buffer3.Length; i++)
         {
             if (buffer3[i] != src[buf.Length + i])
             {
                 return(null);
             }
         }
     }
     return(buf);
 }
예제 #2
0
        public static FormsAuthenticationTicket Decrypt(string encryptedTicket)
        {
            if (string.IsNullOrEmpty(encryptedTicket) || (encryptedTicket.Length > 0x1000))
            {
                throw new ArgumentException(System.Web.SR.GetString("InvalidArgumentValue", new object[] { "encryptedTicket" }));
            }
            Initialize();
            byte[] buf = null;
            if ((encryptedTicket.Length % 2) == 0)
            {
                try
                {
                    buf = MachineKeySection.HexStringToByteArray(encryptedTicket);
                }
                catch
                {
                }
            }
            if (buf == null)
            {
                buf = HttpServerUtility.UrlTokenDecode(encryptedTicket);
            }
            if ((buf == null) || (buf.Length < 1))
            {
                throw new ArgumentException(System.Web.SR.GetString("InvalidArgumentValue", new object[] { "encryptedTicket" }));
            }
            if ((_Protection == FormsProtectionEnum.All) || (_Protection == FormsProtectionEnum.Encryption))
            {
                buf = MachineKeySection.EncryptOrDecryptData(false, buf, null, 0, buf.Length, false, false, IVType.Random);
                if (buf == null)
                {
                    return(null);
                }
            }
            int length = buf.Length;

            if ((_Protection == FormsProtectionEnum.All) || (_Protection == FormsProtectionEnum.Validation))
            {
                if (!MachineKeySection.VerifyHashedData(buf))
                {
                    return(null);
                }
                length -= MachineKeySection.HashSize;
            }
            if (!AppSettings.UseLegacyFormsAuthenticationTicketCompatibility)
            {
                return(FormsAuthenticationTicketSerializer.Deserialize(buf, length));
            }
            int           capacity = (length > 0x1000) ? 0x1000 : length;
            StringBuilder szName   = new StringBuilder(capacity);
            StringBuilder szData   = new StringBuilder(capacity);
            StringBuilder szPath   = new StringBuilder(capacity);

            byte[] pBytes = new byte[4];
            long[] pDates = new long[2];
            if (System.Web.UnsafeNativeMethods.CookieAuthParseTicket(buf, length, szName, capacity, szData, capacity, szPath, capacity, pBytes, pDates) != 0)
            {
                return(null);
            }
            DateTime issueDate = DateTime.FromFileTime(pDates[0]);

            return(new FormsAuthenticationTicket(pBytes[0], szName.ToString(), issueDate, DateTime.FromFileTime(pDates[1]), pBytes[1] != 0, szData.ToString(), szPath.ToString()));
        }