///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// // Helper functions: Hash a password /// <include file='doc\FormsAuthentication.uex' path='docs/doc[@for="FormsAuthentication.HashPasswordForStoringInConfigFile"]/*' /> /// <devdoc> /// Initializes FormsAuthentication by reading /// configuration and getting the cookie values and encryption keys for the given /// application. /// </devdoc> public static String HashPasswordForStoringInConfigFile(String password, String passwordFormat) { if (password == null) { throw new ArgumentNullException("password"); } if (passwordFormat == null) { throw new ArgumentNullException("passwordFormat"); } byte [] bBlob; if (String.Compare(passwordFormat, "sha1", true, CultureInfo.InvariantCulture) == 0) { bBlob = GetMacFromBlob(Encoding.UTF8.GetBytes(password)); } else if (String.Compare(passwordFormat, "md5", true, CultureInfo.InvariantCulture) == 0) { bBlob = GetMD5FromBlob(Encoding.UTF8.GetBytes(password)); } else { throw new ArgumentException(HttpRuntime.FormatResourceString(SR.InvalidArgumentValue, "passwordFormat")); } return(MachineKey.ByteArrayToHexString(bBlob, 0)); }
///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// // Encrypt a ticket /// <include file='doc\FormsAuthentication.uex' path='docs/doc[@for="FormsAuthentication.Encrypt"]/*' /> /// <devdoc> /// Given a FormsAuthenticationTicket, this /// method produces a string containing an encrypted authentication ticket suitable /// for use in an HTTP cookie. /// </devdoc> public static String Encrypt(FormsAuthenticationTicket ticket) { if (ticket == null) { throw new ArgumentNullException("ticket"); } Initialize(); ////////////////////////////////////////////////////////////////////// // Step 1: Make it into a binary blob byte [] bBlob = MakeTicketIntoBinaryBlob(ticket); if (bBlob == null) { return(null); } if (_Protection == FormsProtectionEnum.None) { return(MachineKey.ByteArrayToHexString(bBlob, 0)); } ////////////////////////////////////////////////////////////////////// // Step 2: Get the MAC and add to the blob if (_Protection == FormsProtectionEnum.All || _Protection == FormsProtectionEnum.Validation) { byte [] bMac = MachineKey.HashData(bBlob, null, 0, bBlob.Length); if (bMac == null) { return(null); } Trace("Encrypt: MAC length is: " + bMac.Length); byte [] bAll = new byte[bMac.Length + bBlob.Length]; Buffer.BlockCopy(bBlob, 0, bAll, 0, bBlob.Length); Buffer.BlockCopy(bMac, 0, bAll, bBlob.Length, bMac.Length); if (_Protection == FormsProtectionEnum.Validation) { return(MachineKey.ByteArrayToHexString(bAll, 0)); } bBlob = bAll; } ////////////////////////////////////////////////////////////////////// // Step 3: Do the actual encryption bBlob = MachineKey.EncryptOrDecryptData(true, bBlob, null, 0, bBlob.Length); return(MachineKey.ByteArrayToHexString(bBlob, bBlob.Length)); }