示例#1
0
        public static string Encrypt(FormsAuthenticationTicket ticket)
        {
            if (ticket == null)
            {
                throw new ArgumentNullException("ticket");
            }

            Initialize();
            byte [] ticket_bytes = ticket.ToByteArray();
            if (protection == FormsProtectionEnum.None)
            {
                return(Convert.ToBase64String(ticket_bytes));
            }

            byte []           result = null;
            MachineKeySection config = (MachineKeySection)WebConfigurationManager.GetWebApplicationSection(machineKeyConfigPath);

            if (protection == FormsProtectionEnum.All)
            {
                result = MachineKeySectionUtils.EncryptSign(config, ticket_bytes);
            }
            else if (protection == FormsProtectionEnum.Encryption)
            {
                result = MachineKeySectionUtils.Encrypt(config, ticket_bytes);
            }
            else if (protection == FormsProtectionEnum.Validation)
            {
                result = MachineKeySectionUtils.Sign(config, ticket_bytes);
            }

            return(Convert.ToBase64String(result));
        }
示例#2
0
        public static string Encrypt(FormsAuthenticationTicket ticket)
        {
            if (ticket == null)
            {
                throw new ArgumentNullException("ticket");
            }

            Initialize();
            byte [] ticket_bytes = ticket.ToByteArray();
            if (protection == FormsProtectionEnum.None)
            {
                return(GetHexString(ticket_bytes));
            }

            byte [] result = ticket_bytes;
#if NET_2_0
            MachineKeySection config = (MachineKeySection)WebConfigurationManager.GetWebApplicationSection(machineKeyConfigPath);
#else
            MachineKeyConfig config = HttpContext.GetAppConfig(machineKeyConfigPath) as MachineKeyConfig;
#endif
            bool all = (protection == FormsProtectionEnum.All);
            if (all || protection == FormsProtectionEnum.Validation)
            {
                byte [] valid_bytes = null;
#if NET_2_0
                byte [] vk = MachineKeySectionUtils.ValidationKeyBytes(config);
#else
                byte [] vk = config.ValidationKey;
#endif
                byte [] mix = new byte [ticket_bytes.Length + vk.Length];
                Buffer.BlockCopy(ticket_bytes, 0, mix, 0, ticket_bytes.Length);
                Buffer.BlockCopy(vk, 0, mix, result.Length, vk.Length);

                switch (
#if NET_2_0
                    config.Validation
#else
                    config.ValidationType
#endif
                    )
                {
		public static string Encrypt (FormsAuthenticationTicket ticket)
		{
			if (ticket == null)
				throw new ArgumentNullException ("ticket");

			Initialize ();
			byte [] ticket_bytes = ticket.ToByteArray ();
			if (protection == FormsProtectionEnum.None)
				return GetHexString (ticket_bytes);

			byte [] result = ticket_bytes;
			MachineKeyConfig config = HttpContext.GetAppConfig ("system.web/machineKey") as MachineKeyConfig;
			bool all = (protection == FormsProtectionEnum.All);
			if (all || protection == FormsProtectionEnum.Validation) {
				byte [] valid_bytes = null;
				byte [] vk = config.ValidationKey;
				byte [] mix = new byte [ticket_bytes.Length + vk.Length];
				Buffer.BlockCopy (ticket_bytes, 0, mix, 0, ticket_bytes.Length);
				Buffer.BlockCopy (vk, 0, mix, result.Length, vk.Length);

				switch (config.ValidationType) {
				case MachineKeyValidation.MD5:
					valid_bytes = MD5.Create ().ComputeHash (mix);
					break;
				// From MS docs: "When 3DES is specified, forms authentication defaults to SHA1"
				case MachineKeyValidation.TripleDES:
				case MachineKeyValidation.SHA1:
					valid_bytes = SHA1.Create ().ComputeHash (mix);
					break;
				}

				int tlen = ticket_bytes.Length;
				int vlen = valid_bytes.Length;
				result = new byte [tlen + vlen];
				Buffer.BlockCopy (ticket_bytes, 0, result, 0, tlen);
				Buffer.BlockCopy (valid_bytes, 0, result, tlen, vlen);
			}

			if (all || protection == FormsProtectionEnum.Encryption) {
				ICryptoTransform encryptor;
				encryptor = new TripleDESCryptoServiceProvider().CreateEncryptor (config.DecryptionKey192Bits, init_vector);
				result = encryptor.TransformFinalBlock (result, 0, result.Length);
			}

			return GetHexString (result);
		}
示例#4
0
		public static string Encrypt (FormsAuthenticationTicket ticket)
		{
			if (ticket == null)
				throw new ArgumentNullException ("ticket");

			Initialize ();
			byte [] ticket_bytes = ticket.ToByteArray ();
			if (protection == FormsProtectionEnum.None)
				return Convert.ToBase64String (ticket_bytes);

			byte [] result = null;
			MachineKeySection config = (MachineKeySection) WebConfigurationManager.GetWebApplicationSection (machineKeyConfigPath);

			if (protection == FormsProtectionEnum.All) {
				result = MachineKeySectionUtils.EncryptSign (config, ticket_bytes);
			} else if (protection == FormsProtectionEnum.Encryption) {
				result = MachineKeySectionUtils.Encrypt (config, ticket_bytes);
			} else if (protection == FormsProtectionEnum.Validation) {
				result = MachineKeySectionUtils.Sign (config, ticket_bytes);
			}

			return Convert.ToBase64String (result);
		}