Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / Security / CookieProtection.cs / 1305376 / CookieProtection.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.Security
{
using System.Security.Cryptography;
using System.Web.Configuration;
using System.Web.Management;
public enum CookieProtection
{
None, Validation, Encryption, All
}
internal class CookieProtectionHelper
{
internal static string Encode (CookieProtection cookieProtection, byte [] buf, int count)
{
if (cookieProtection == CookieProtection.All || cookieProtection == CookieProtection.Validation)
{
byte[] bMac = MachineKeySection.HashData (buf, null, 0, count);
if (bMac == null)
return null;
if (buf.Length >= count + bMac.Length)
{
Buffer.BlockCopy (bMac, 0, buf, count, bMac.Length);
}
else
{
byte[] bTemp = buf;
buf = new byte[count + bMac.Length];
Buffer.BlockCopy (bTemp, 0, buf, 0, count);
Buffer.BlockCopy (bMac, 0, buf, count, bMac.Length);
}
count += bMac.Length;
}
if (cookieProtection == CookieProtection.All || cookieProtection == CookieProtection.Encryption)
{
buf = MachineKeySection.EncryptOrDecryptData (true, buf, null, 0, count);
count = buf.Length;
}
if (count < buf.Length)
{
byte[] bTemp = buf;
buf = new byte[count];
Buffer.BlockCopy (bTemp, 0, buf, 0, count);
}
return HttpServerUtility.UrlTokenEncode(buf);
}
internal static byte[] Decode (CookieProtection cookieProtection, string data)
{
byte [] buf = HttpServerUtility.UrlTokenDecode(data);
if (buf == null || cookieProtection == CookieProtection.None)
return buf;
if (cookieProtection == CookieProtection.All || cookieProtection == CookieProtection.Encryption)
{
buf = MachineKeySection.EncryptOrDecryptData (false, buf, null, 0, buf.Length);
if (buf == null)
return null;
}
if (cookieProtection == CookieProtection.All || cookieProtection == CookieProtection.Validation)
return MachineKeySection.GetUnHashedData(buf);
return buf;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- HtmlInputControl.cs
- AudioSignalProblemOccurredEventArgs.cs
- ColumnResizeAdorner.cs
- IInstanceTable.cs
- StrongTypingException.cs
- EntityStoreSchemaFilterEntry.cs
- Point3DAnimationBase.cs
- PolyLineSegment.cs
- HandleCollector.cs
- ToolStripLabel.cs
- SecureStringHasher.cs
- ShimAsPublicXamlType.cs
- VersionPair.cs
- MergablePropertyAttribute.cs
- X509InitiatorCertificateServiceElement.cs
- WinFormsComponentEditor.cs
- PenThreadPool.cs
- Table.cs
- ModuleConfigurationInfo.cs
- GeometryDrawing.cs
- AspNetSynchronizationContext.cs
- RedBlackList.cs
- FreezableOperations.cs
- AssemblyAssociatedContentFileAttribute.cs
- PathData.cs
- Privilege.cs
- FactoryGenerator.cs
- NumericPagerField.cs
- GroupBox.cs
- designeractionlistschangedeventargs.cs
- CommandLineParser.cs
- ChangeNode.cs
- UnsafeNativeMethods.cs
- _LocalDataStore.cs
- ContourSegment.cs
- RankException.cs
- ReflectionHelper.cs
- OpenFileDialog.cs
- JournalEntry.cs
- PropertyGridDesigner.cs
- ObjectHandle.cs
- SystemColorTracker.cs
- TablePattern.cs
- ActivityExecutionFilter.cs
- HttpChannelListener.cs
- ButtonBase.cs
- SimpleType.cs
- KeyboardNavigation.cs
- DataGridAutomationPeer.cs
- XPathParser.cs
- PageThemeParser.cs
- XmlBinaryReaderSession.cs
- CustomSignedXml.cs
- OneOfConst.cs
- EntityConnectionStringBuilder.cs
- NonVisualControlAttribute.cs
- PipeStream.cs
- SplitterCancelEvent.cs
- Vector3D.cs
- ContainerParaClient.cs
- PolicyUnit.cs
- OdbcFactory.cs
- EventInfo.cs
- x509utils.cs
- SystemDropShadowChrome.cs
- ColumnMap.cs
- WinHttpWebProxyFinder.cs
- XpsDocument.cs
- DrawingContextWalker.cs
- FormsIdentity.cs
- VectorKeyFrameCollection.cs
- Pkcs7Signer.cs
- ScaleTransform3D.cs
- DrawingServices.cs
- DesignerRegion.cs
- TraceInternal.cs
- InfoCardBaseException.cs
- ADConnectionHelper.cs
- Stroke.cs
- WindowsAltTab.cs
- ToolboxItemAttribute.cs
- GenericTextProperties.cs
- NonDualMessageSecurityOverHttpElement.cs
- SafeEventLogReadHandle.cs
- TextureBrush.cs
- DataControlImageButton.cs
- Schema.cs
- MDIControlStrip.cs
- ZipArchive.cs
- MsmqChannelFactoryBase.cs
- LogicalExpr.cs
- RegionIterator.cs
- HandlerBase.cs
- ModuleConfigurationInfo.cs
- DetailsViewDeletedEventArgs.cs
- FixedSOMImage.cs
- TableHeaderCell.cs
- ProxyGenerator.cs
- Transaction.cs
- DocumentStatusResources.cs