Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / Security / WindowsAuthenticationModule.cs / 1305376 / WindowsAuthenticationModule.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- /* * WindowsAuthenticationModule class * * Copyright (c) 1999 Microsoft Corporation */ namespace System.Web.Security { using System.Web; using System.Web.Configuration; using System.Security.Principal; using System.Security.Permissions; using System.Globalization; using System.Web.Management; using System.Web.Util; using System.Web.Hosting; ////// public sealed class WindowsAuthenticationModule : IHttpModule { private WindowsAuthenticationEventHandler _eventHandler; private static bool _fAuthChecked; private static bool _fAuthRequired; private static WindowsIdentity _anonymousIdentity; private static WindowsPrincipal _anonymousPrincipal; ////// Allows ASP.NET applications to use Windows/IIS authentication. /// ////// [SecurityPermission(SecurityAction.Demand, Unrestricted=true)] public WindowsAuthenticationModule() { } ////// Initializes a new instance of the ////// class. /// /// This is a global.asax event that must be /// named WindowsAuthenticate_OnAuthenticate event. It's used primarily to attach a /// custom IPrincipal object to the context. /// public event WindowsAuthenticationEventHandler Authenticate { add { _eventHandler += value; } remove { _eventHandler -= value; } } ////// public void Dispose() { } ///[To be supplied.] ////// public void Init(HttpApplication app) { app.AuthenticateRequest += new EventHandler(this.OnEnter); } //////////////////////////////////////////////////////////// // OnAuthenticate: Custom Authentication modules can override // this method to create a custom IPrincipal object from // a WindowsIdentity ///[To be supplied.] ////// Calls the /// WindowsAuthentication_OnAuthenticate handler if one exists. /// void OnAuthenticate(WindowsAuthenticationEventArgs e) { //////////////////////////////////////////////////////////// // If there are event handlers, invoke the handlers if (_eventHandler != null) _eventHandler(this, e); if (e.Context.User == null) { if (e.User != null) e.Context.User = e.User; else if (e.Identity == _anonymousIdentity) e.Context.SetPrincipalNoDemand(_anonymousPrincipal, false /*needToSetNativePrincipal*/); else e.Context.SetPrincipalNoDemand(new WindowsPrincipal(e.Identity), false /*needToSetNativePrincipal*/); } } //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// // Methods for internal implementation ////// /// [SecurityPermission(SecurityAction.Assert, UnmanagedCode = true, ControlPrincipal = true)] void OnEnter(Object source, EventArgs eventArgs) { if (!IsEnabled) return; HttpApplication app = (HttpApplication)source; HttpContext context = app.Context;; WindowsIdentity identity = null; ////////////////////////////////////////////////////////////////// // Step 2: Create a Windows Identity from the credentials from IIS if (HttpRuntime.UseIntegratedPipeline) { // The native WindowsAuthenticationModule sets the user principal in IIS7WorkerRequest.SynchronizeVariables. // The managed WindowsAuthenticationModule provides backward compatibility by rasing the OnAuthenticate event. WindowsPrincipal user = context.User as WindowsPrincipal; if (user != null) { // identity will be null if this is not a WindowsIdentity identity = user.Identity as WindowsIdentity; // clear Context.User for backward compatibility (it will be set in OnAuthenticate) context.SetPrincipalNoDemand(null, false /*needToSetNativePrincipal*/); } } else { String strLogonUser = context.WorkerRequest.GetServerVariable("LOGON_USER"); String strAuthType = context.WorkerRequest.GetServerVariable("AUTH_TYPE"); if (strLogonUser == null) { strLogonUser = String.Empty; } if (strAuthType == null) { strAuthType = String.Empty; } if (strLogonUser.Length == 0 && (strAuthType.Length == 0 || StringUtil.EqualsIgnoreCase(strAuthType, "basic"))) { //////////////////////////////////////////////////////// // Step 2a: Use the anonymous identity identity = _anonymousIdentity; } else { identity = new WindowsIdentity( context.WorkerRequest.GetUserToken(), strAuthType, WindowsAccountType.Normal, true); } } /////////////////////////////////////////////////////////////////////////////////// // Step 3: Call OnAuthenticate to create IPrincipal for this request. if (identity != null) { OnAuthenticate( new WindowsAuthenticationEventArgs(identity, context) ); } } internal static IPrincipal AnonymousPrincipal { get { return _anonymousPrincipal; } } internal static bool IsEnabled { get { if (!_fAuthChecked) { _fAuthRequired = (AuthenticationConfig.Mode == AuthenticationMode.Windows); if (_fAuthRequired) { _anonymousIdentity = WindowsIdentity.GetAnonymous(); _anonymousPrincipal = new WindowsPrincipal(_anonymousIdentity); } _fAuthChecked = true; } return _fAuthRequired; } } } } // 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
- TypeConvertions.cs
- CompositeCollection.cs
- GeneratedContractType.cs
- RedirectionProxy.cs
- ResumeStoryboard.cs
- CircleEase.cs
- ShapeTypeface.cs
- Operand.cs
- LinkLabelLinkClickedEvent.cs
- UIElement3DAutomationPeer.cs
- EntityTypeEmitter.cs
- Transform3DGroup.cs
- ConnectionStringSettings.cs
- sqlinternaltransaction.cs
- DebugHandleTracker.cs
- EntityContainerAssociationSet.cs
- DataGridViewUtilities.cs
- StyleSheetDesigner.cs
- LineUtil.cs
- FrameworkReadOnlyPropertyMetadata.cs
- DurableRuntimeValidator.cs
- WriteTimeStream.cs
- PtsPage.cs
- PanelStyle.cs
- MouseOverProperty.cs
- GroupBoxAutomationPeer.cs
- EncryptedKeyIdentifierClause.cs
- Matrix3DConverter.cs
- SettingsPropertyWrongTypeException.cs
- TextServicesDisplayAttribute.cs
- HotSpot.cs
- DataGridViewRowsRemovedEventArgs.cs
- ErrorFormatter.cs
- BaseTemplateParser.cs
- ISSmlParser.cs
- ResourceLoader.cs
- MutexSecurity.cs
- MemoryMappedViewAccessor.cs
- ChtmlTextWriter.cs
- ConfigXmlDocument.cs
- EtwTrackingParticipant.cs
- ApplicationSettingsBase.cs
- ErrorHandlerModule.cs
- ScaleTransform.cs
- DataGridViewCellStyleEditor.cs
- Frame.cs
- ListControlStringCollectionEditor.cs
- LoadItemsEventArgs.cs
- RijndaelManagedTransform.cs
- SerializeAbsoluteContext.cs
- UrlMappingsModule.cs
- LinqDataView.cs
- ProgressBarHighlightConverter.cs
- CompilerInfo.cs
- BindingParameterCollection.cs
- CodeDelegateInvokeExpression.cs
- OutputScope.cs
- AsymmetricKeyExchangeDeformatter.cs
- ToolStripProgressBar.cs
- WindowsTab.cs
- XmlTextReaderImplHelpers.cs
- KeySplineConverter.cs
- DataStorage.cs
- GenericTypeParameterBuilder.cs
- DrawingDrawingContext.cs
- PeerObject.cs
- PointAnimationUsingPath.cs
- SmtpCommands.cs
- WindowsAuthenticationEventArgs.cs
- ToolboxDataAttribute.cs
- ColorTranslator.cs
- EmptyStringExpandableObjectConverter.cs
- GlyphRunDrawing.cs
- WindowsToolbarAsMenu.cs
- TrackingMemoryStream.cs
- CompilationUtil.cs
- TaiwanLunisolarCalendar.cs
- ReadWriteObjectLock.cs
- WebScriptEndpointElement.cs
- DataControlPagerLinkButton.cs
- ReachPageContentSerializer.cs
- WindowClosedEventArgs.cs
- Scene3D.cs
- DependencyPropertyChangedEventArgs.cs
- AttributeTableBuilder.cs
- EventlogProvider.cs
- MimeReturn.cs
- ListChangedEventArgs.cs
- Compiler.cs
- TTSEngineTypes.cs
- ClientProxyGenerator.cs
- CellNormalizer.cs
- BooleanAnimationUsingKeyFrames.cs
- ApplicationManager.cs
- DataBoundControlHelper.cs
- DataGridHelper.cs
- Deserializer.cs
- PropertyConverter.cs
- RegularExpressionValidator.cs
- WebReferenceCollection.cs