当前位置: 首页>>代码示例>>C#>>正文


C# FormsAuthenticationTicket.ToByteArray方法代码示例

本文整理汇总了C#中System.Web.Security.FormsAuthenticationTicket.ToByteArray方法的典型用法代码示例。如果您正苦于以下问题:C# FormsAuthenticationTicket.ToByteArray方法的具体用法?C# FormsAuthenticationTicket.ToByteArray怎么用?C# FormsAuthenticationTicket.ToByteArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Web.Security.FormsAuthenticationTicket的用法示例。


在下文中一共展示了FormsAuthenticationTicket.ToByteArray方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Encrypt

		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);
		}
开发者ID:jjenki11,项目名称:blaze-chem-rendering,代码行数:46,代码来源:FormsAuthentication.cs

示例2: Encrypt

		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);
		}
开发者ID:Profit0004,项目名称:mono,代码行数:23,代码来源:FormsAuthentication.cs


注:本文中的System.Web.Security.FormsAuthenticationTicket.ToByteArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。