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
- RuntimeHelpers.cs
- ApplicationTrust.cs
- ResumeStoryboard.cs
- PrivateFontCollection.cs
- ModuleBuilder.cs
- HeaderCollection.cs
- LayoutEvent.cs
- HMACSHA384.cs
- DefaultHttpHandler.cs
- XpsFixedPageReaderWriter.cs
- PropertyMappingExceptionEventArgs.cs
- DefaultCommandExtensionCallback.cs
- HitTestWithGeometryDrawingContextWalker.cs
- ExpressionBinding.cs
- TreeWalkHelper.cs
- HostingPreferredMapPath.cs
- TraceContext.cs
- FileSystemEventArgs.cs
- XPSSignatureDefinition.cs
- PeerNearMe.cs
- HttpListenerRequest.cs
- TextElementEnumerator.cs
- WorkflowTraceTransfer.cs
- QuaternionRotation3D.cs
- OleDbCommand.cs
- CacheRequest.cs
- ListViewInsertEventArgs.cs
- EntityAdapter.cs
- StreamedWorkflowDefinitionContext.cs
- WebServiceTypeData.cs
- SizeAnimation.cs
- DefaultTraceListener.cs
- AuthenticationModulesSection.cs
- SHA256.cs
- WindowsFormsSynchronizationContext.cs
- ProtocolsSection.cs
- MDIWindowDialog.cs
- WebPartZoneBase.cs
- Polygon.cs
- GridViewDeleteEventArgs.cs
- MarkupObject.cs
- SetterBaseCollection.cs
- Command.cs
- QueryModel.cs
- DataTableTypeConverter.cs
- SqlDataRecord.cs
- InputReport.cs
- RuleProcessor.cs
- SafeRegistryHandle.cs
- NotifyInputEventArgs.cs
- TemplateControlParser.cs
- SqlDataSourceSelectingEventArgs.cs
- NameValueSectionHandler.cs
- XmlConvert.cs
- CellTreeNodeVisitors.cs
- QueryStringParameter.cs
- XmlConverter.cs
- PeerObject.cs
- Nullable.cs
- XmlSerializer.cs
- dbenumerator.cs
- CodeBinaryOperatorExpression.cs
- Visual3DCollection.cs
- ConversionValidationRule.cs
- _DomainName.cs
- HwndHostAutomationPeer.cs
- ProcessHostConfigUtils.cs
- ButtonStandardAdapter.cs
- CheckBoxFlatAdapter.cs
- FormatterServicesNoSerializableCheck.cs
- DynamicMethod.cs
- tibetanshape.cs
- AssociationTypeEmitter.cs
- SystemInfo.cs
- PrePostDescendentsWalker.cs
- OdbcErrorCollection.cs
- TransformProviderWrapper.cs
- WSTrust.cs
- ToolboxComponentsCreatedEventArgs.cs
- EqualityComparer.cs
- BaseUriHelper.cs
- SvcFileManager.cs
- DataGridViewCellCollection.cs
- RtfToXamlReader.cs
- HttpCookie.cs
- TableRowGroup.cs
- ReadonlyMessageFilter.cs
- Attributes.cs
- StandardOleMarshalObject.cs
- XmlTextAttribute.cs
- XmlQueryStaticData.cs
- OleDbErrorCollection.cs
- ProjectionNode.cs
- WebPartEditorOkVerb.cs
- CodeGeneratorOptions.cs
- XmlDataSourceNodeDescriptor.cs
- DllHostedComPlusServiceHost.cs
- DataViewSetting.cs
- HasCopySemanticsAttribute.cs
- COM2ComponentEditor.cs