System.Web.Security.MachineKey.Encode C# (CSharp) Method

Encode() public static method

public static Encode ( byte data, MachineKeyProtection protectionOption ) : string
data byte
protectionOption MachineKeyProtection
return string
		public static string Encode (byte[] data, MachineKeyProtection protectionOption)
		{
			if (data == null)
				throw new ArgumentNullException ("data");

			var config = WebConfigurationManager.GetWebApplicationSection ("system.web/machineKey") as MachineKeySection;
			byte[] result;
			switch (protectionOption) {
				case MachineKeyProtection.All:
					result = MachineKeySectionUtils.EncryptSign (config, data);
					break;

				case MachineKeyProtection.Encryption:
					result = MachineKeySectionUtils.Encrypt (config, data);
					break;

				case MachineKeyProtection.Validation:
					result = MachineKeySectionUtils.Sign (config, data);
					break;

				default:
					return String.Empty;
			}
			
			return MachineKeySectionUtils.GetHexString (result);
		}

Usage Example

Example #1
0
        ///30Mar16 Addedd by Keyur Desai, Infosys, START

        /// <summary>
        /// Uses .NET 4.0/4.5 API.
        /// With MachineKeyProtection.Encryption option.
        /// </summary>
        /// <param name="input">String to be encrypted.</param>
        /// <returns>Encrypted string.</returns>
        internal static string Protect(string input)
        {
            byte[] bytesToProtect = System.Text.Encoding.Unicode.GetBytes(input);
#if NET40
            return(MachineKey.Encode(bytesToProtect, MachineKeyProtection.Encryption));
#else
            byte[] protectedBytes = MachineKey.Protect(bytesToProtect);
            return(Convert.ToBase64String(protectedBytes));
#endif
        }