Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / Security / MachineKey.cs / 1305376 / MachineKey.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
* MachineKey
*
* Copyright (c) 2009 Microsoft Corporation
*/
namespace System.Web.Security {
using System.Web.Configuration;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
public enum MachineKeyProtection {
All,
Encryption,
Validation
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
public static class MachineKey {
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
public static string Encode(byte[] data, MachineKeyProtection protectionOption) {
if (data == null)
throw new ArgumentNullException("data");
//////////////////////////////////////////////////////////////////////
// Step 1: Get the MAC and add to the blob
if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Validation) {
byte [] bHash = MachineKeySection.HashData(data, null, 0, data.Length);
byte [] bAll = new byte[bHash.Length + data.Length];
Buffer.BlockCopy(data, 0, bAll, 0, data.Length);
Buffer.BlockCopy(bHash, 0, bAll, data.Length, bHash.Length);
data = bAll;
}
if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Encryption) {
//////////////////////////////////////////////////////////////////////
// Step 2: Encryption
data = MachineKeySection.EncryptOrDecryptData(true, data, null, 0, data.Length, false, false, IVType.Random);
}
//////////////////////////////////////////////////////////////////////
// Step 3: Covert the buffer to HEX string and return it
return MachineKeySection.ByteArrayToHexString(data, 0);
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
public static byte [] Decode(string encodedData, MachineKeyProtection protectionOption) {
if (encodedData == null)
throw new ArgumentNullException("encodedData");
if ((encodedData.Length % 2) != 0)
throw new ArgumentException(null, "encodedData");
byte [] data = null;
try {
//////////////////////////////////////////////////////////////////////
// Step 1: Covert the HEX string to byte array
data = MachineKeySection.HexStringToByteArray(encodedData);
} catch {
throw new ArgumentException(null, "encodedData");
}
if (data == null || data.Length < 1)
throw new ArgumentException(null, "encodedData");
if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Encryption) {
//////////////////////////////////////////////////////////////////
// Step 2: Decrypt the data
data = MachineKeySection.EncryptOrDecryptData(false, data, null, 0, data.Length, false, false, IVType.Random);
if (data == null)
return null;
}
if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Validation) {
//////////////////////////////////////////////////////////////////
// Step 3a: Remove the hash from the end of the data
if (data.Length < MachineKeySection.HashSize)
return null;
byte [] originalData = data;
data = new byte[originalData.Length - MachineKeySection.HashSize];
Buffer.BlockCopy(originalData, 0, data, 0, data.Length);
//////////////////////////////////////////////////////////////////
// Step 3b: Calculate the hash and make sure it matches
byte [] bHash = MachineKeySection.HashData(data, null, 0, data.Length);
if (bHash == null || bHash.Length != MachineKeySection.HashSize)
return null; // Sizes don't match
for(int iter=0; iter
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Command.cs
- HMAC.cs
- COM2Enum.cs
- ResourceIDHelper.cs
- IsolatedStorageFile.cs
- PointCollection.cs
- FlatButtonAppearance.cs
- CodeGeneratorAttribute.cs
- TrackingMemoryStreamFactory.cs
- Atom10FormatterFactory.cs
- InvalidateEvent.cs
- DeviceSpecificChoiceCollection.cs
- UnmanagedMemoryStreamWrapper.cs
- Expression.cs
- TreeViewDesigner.cs
- PathGeometry.cs
- DeflateInput.cs
- TemplateControl.cs
- SamlAction.cs
- PropertyTabChangedEvent.cs
- Helper.cs
- DefaultTextStore.cs
- SvcMapFile.cs
- XPathAxisIterator.cs
- ReceiveCompletedEventArgs.cs
- DefinitionBase.cs
- DataFormats.cs
- MessageBox.cs
- TraceSwitch.cs
- UpdateProgress.cs
- WorkflowStateRollbackService.cs
- XmlSchemaException.cs
- UnmanagedMemoryStreamWrapper.cs
- InfocardExtendedInformationCollection.cs
- CriticalFinalizerObject.cs
- FormatConvertedBitmap.cs
- HandlerMappingMemo.cs
- DrawListViewSubItemEventArgs.cs
- DllNotFoundException.cs
- ManagedIStream.cs
- MsmqBindingMonitor.cs
- CodeGeneratorOptions.cs
- ModulesEntry.cs
- StickyNoteContentControl.cs
- IndividualDeviceConfig.cs
- ResourceProviderFactory.cs
- OutputCacheProfile.cs
- SchemaImporter.cs
- HelpPage.cs
- JsonQNameDataContract.cs
- CustomCategoryAttribute.cs
- NewArrayExpression.cs
- FileSystemWatcher.cs
- ContentPresenter.cs
- CustomCredentialPolicy.cs
- DataRecordInfo.cs
- ConnectorSelectionGlyph.cs
- WebConfigurationHost.cs
- WindowsListViewItemStartMenu.cs
- Delegate.cs
- EngineSiteSapi.cs
- DriveInfo.cs
- AspCompat.cs
- IncomingWebRequestContext.cs
- CharStorage.cs
- WsdlBuildProvider.cs
- ResolvedKeyFrameEntry.cs
- ClientEventManager.cs
- DataServiceContext.cs
- StatusBarPanel.cs
- AssemblyAttributes.cs
- HttpModuleCollection.cs
- CompiledRegexRunner.cs
- CodeExporter.cs
- SQLInt16.cs
- KeyedCollection.cs
- DataGridViewElement.cs
- GenericTypeParameterConverter.cs
- InvalidOleVariantTypeException.cs
- panel.cs
- SystemPens.cs
- CssClassPropertyAttribute.cs
- TextEditorContextMenu.cs
- OrthographicCamera.cs
- Label.cs
- MultiView.cs
- ComContractElement.cs
- Control.cs
- MatrixTransform.cs
- GatewayDefinition.cs
- PatternMatcher.cs
- PersonalizableAttribute.cs
- HttpRequestCacheValidator.cs
- Regex.cs
- DataGridViewCheckBoxCell.cs
- Cursor.cs
- coordinatorscratchpad.cs
- TraceSection.cs
- MgmtConfigurationRecord.cs
- AmbientLight.cs