Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / DEVDIV / depot / DevDiv / releases / whidbey / QFE / ndp / fx / src / xsp / System / Web / Security / RoleManagerModule.cs / 4 / RoleManagerModule.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
* RoleManagerModule class
*
* Copyright (c) 1999 Microsoft Corporation
*/
namespace System.Web.Security {
using System.Collections;
using System.Security.Principal;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Configuration;
using System.Web.Caching;
using System.Web.Util;
///
/// [To be supplied.]
///
[AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
public sealed class RoleManagerModule : IHttpModule {
private const int MAX_COOKIE_LENGTH = 4096;
private RoleManagerEventHandler _eventHandler;
///
///
/// Initializes a new instance of the
/// class.
///
///
[SecurityPermission(SecurityAction.Demand, Unrestricted=true)]
public RoleManagerModule() {
}
public event RoleManagerEventHandler GetRoles {
add {
HttpRuntime.CheckAspNetHostingPermission(AspNetHostingPermissionLevel.Low, SR.Feature_not_supported_at_this_level);
_eventHandler += value;
}
remove {
_eventHandler -= value;
}
}
///
/// [To be supplied.]
///
public void Dispose() {
}
///
/// [To be supplied.]
///
public void Init(HttpApplication app) {
// for IIS 7, skip wireup of these delegates altogether unless the
// feature is enabled for this application
// this avoids the initial OnEnter transition unless it's needed
if (Roles.Enabled) {
app.PostAuthenticateRequest += new EventHandler(this.OnEnter);
app.EndRequest += new EventHandler(this.OnLeave);
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
///
/// [To be supplied.]
///
private void OnEnter(Object source, EventArgs eventArgs) {
if (!Roles.Enabled) {
if (HttpRuntime.UseIntegratedPipeline) {
((HttpApplication)source).Context.DisableNotifications(RequestNotification.EndRequest, 0);
}
return;
}
HttpApplication app = (HttpApplication)source;
HttpContext context = app.Context;
if (_eventHandler != null) {
RoleManagerEventArgs e = new RoleManagerEventArgs(context);
_eventHandler(this, e);
if (e.RolesPopulated)
return;
}
Debug.Assert(null != context.User, "null != context.User");
if (Roles.CacheRolesInCookie)
{
if (context.User.Identity.IsAuthenticated && (!Roles.CookieRequireSSL || context.Request.IsSecureConnection))
{
// Try to create from cookie
try
{
HttpCookie cookie = context.Request.Cookies[Roles.CookieName];
if (cookie != null)
{
string cookieValue = cookie.Value;
// Ignore cookies that are too long
if (cookieValue != null && cookieValue.Length > MAX_COOKIE_LENGTH) {
Roles.DeleteCookie();
}
else {
if (!String.IsNullOrEmpty(Roles.CookiePath) && Roles.CookiePath != "/") {
cookie.Path = Roles.CookiePath;
}
cookie.Domain = Roles.Domain;
context.User = new RolePrincipal(context.User.Identity, cookieValue);
}
}
}
catch { } // skip exceptions
}
else
{
if (context.Request.Cookies[Roles.CookieName] != null)
Roles.DeleteCookie();
// if we're not using cookie caching, we don't need the EndRequest
// event and can suppress it
if (HttpRuntime.UseIntegratedPipeline) {
context.DisableNotifications(RequestNotification.EndRequest, 0);
}
}
}
if (!(context.User is RolePrincipal))
context.User = new RolePrincipal(context.User.Identity);
Thread.CurrentPrincipal = context.User;
}
private void OnLeave(Object source, EventArgs eventArgs) {
HttpApplication app;
HttpContext context;
app = (HttpApplication)source;
context = app.Context;
if (!Roles.Enabled || !Roles.CacheRolesInCookie || context.Response.HeadersWritten)
return;
if (context.User == null || !(context.User is RolePrincipal) || !context.User.Identity.IsAuthenticated)
return;
if (Roles.CookieRequireSSL && !context.Request.IsSecureConnection)
{ // if cookie is sent, then clear it
if (context.Request.Cookies[Roles.CookieName] != null)
Roles.DeleteCookie();
return;
}
RolePrincipal rp = (RolePrincipal) context.User;
if (rp.CachedListChanged && context.Request.Browser.Cookies)
{
string s = rp.ToEncryptedTicket();
if (string.IsNullOrEmpty(s) || s.Length > MAX_COOKIE_LENGTH) {
Roles.DeleteCookie();
} else {
HttpCookie cookie = new HttpCookie(Roles.CookieName, s);
cookie.HttpOnly = true;
cookie.Path = Roles.CookiePath;
cookie.Domain = Roles.Domain;
if (Roles.CreatePersistentCookie)
cookie.Expires = rp.ExpireDate;
cookie.Secure = Roles.CookieRequireSSL;
context.Response.Cookies.Add(cookie);
}
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
* RoleManagerModule class
*
* Copyright (c) 1999 Microsoft Corporation
*/
namespace System.Web.Security {
using System.Collections;
using System.Security.Principal;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Configuration;
using System.Web.Caching;
using System.Web.Util;
///
/// [To be supplied.]
///
[AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
public sealed class RoleManagerModule : IHttpModule {
private const int MAX_COOKIE_LENGTH = 4096;
private RoleManagerEventHandler _eventHandler;
///
///
/// Initializes a new instance of the
/// class.
///
///
[SecurityPermission(SecurityAction.Demand, Unrestricted=true)]
public RoleManagerModule() {
}
public event RoleManagerEventHandler GetRoles {
add {
HttpRuntime.CheckAspNetHostingPermission(AspNetHostingPermissionLevel.Low, SR.Feature_not_supported_at_this_level);
_eventHandler += value;
}
remove {
_eventHandler -= value;
}
}
///
/// [To be supplied.]
///
public void Dispose() {
}
///
/// [To be supplied.]
///
public void Init(HttpApplication app) {
// for IIS 7, skip wireup of these delegates altogether unless the
// feature is enabled for this application
// this avoids the initial OnEnter transition unless it's needed
if (Roles.Enabled) {
app.PostAuthenticateRequest += new EventHandler(this.OnEnter);
app.EndRequest += new EventHandler(this.OnLeave);
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
///
/// [To be supplied.]
///
private void OnEnter(Object source, EventArgs eventArgs) {
if (!Roles.Enabled) {
if (HttpRuntime.UseIntegratedPipeline) {
((HttpApplication)source).Context.DisableNotifications(RequestNotification.EndRequest, 0);
}
return;
}
HttpApplication app = (HttpApplication)source;
HttpContext context = app.Context;
if (_eventHandler != null) {
RoleManagerEventArgs e = new RoleManagerEventArgs(context);
_eventHandler(this, e);
if (e.RolesPopulated)
return;
}
Debug.Assert(null != context.User, "null != context.User");
if (Roles.CacheRolesInCookie)
{
if (context.User.Identity.IsAuthenticated && (!Roles.CookieRequireSSL || context.Request.IsSecureConnection))
{
// Try to create from cookie
try
{
HttpCookie cookie = context.Request.Cookies[Roles.CookieName];
if (cookie != null)
{
string cookieValue = cookie.Value;
// Ignore cookies that are too long
if (cookieValue != null && cookieValue.Length > MAX_COOKIE_LENGTH) {
Roles.DeleteCookie();
}
else {
if (!String.IsNullOrEmpty(Roles.CookiePath) && Roles.CookiePath != "/") {
cookie.Path = Roles.CookiePath;
}
cookie.Domain = Roles.Domain;
context.User = new RolePrincipal(context.User.Identity, cookieValue);
}
}
}
catch { } // skip exceptions
}
else
{
if (context.Request.Cookies[Roles.CookieName] != null)
Roles.DeleteCookie();
// if we're not using cookie caching, we don't need the EndRequest
// event and can suppress it
if (HttpRuntime.UseIntegratedPipeline) {
context.DisableNotifications(RequestNotification.EndRequest, 0);
}
}
}
if (!(context.User is RolePrincipal))
context.User = new RolePrincipal(context.User.Identity);
Thread.CurrentPrincipal = context.User;
}
private void OnLeave(Object source, EventArgs eventArgs) {
HttpApplication app;
HttpContext context;
app = (HttpApplication)source;
context = app.Context;
if (!Roles.Enabled || !Roles.CacheRolesInCookie || context.Response.HeadersWritten)
return;
if (context.User == null || !(context.User is RolePrincipal) || !context.User.Identity.IsAuthenticated)
return;
if (Roles.CookieRequireSSL && !context.Request.IsSecureConnection)
{ // if cookie is sent, then clear it
if (context.Request.Cookies[Roles.CookieName] != null)
Roles.DeleteCookie();
return;
}
RolePrincipal rp = (RolePrincipal) context.User;
if (rp.CachedListChanged && context.Request.Browser.Cookies)
{
string s = rp.ToEncryptedTicket();
if (string.IsNullOrEmpty(s) || s.Length > MAX_COOKIE_LENGTH) {
Roles.DeleteCookie();
} else {
HttpCookie cookie = new HttpCookie(Roles.CookieName, s);
cookie.HttpOnly = true;
cookie.Path = Roles.CookiePath;
cookie.Domain = Roles.Domain;
if (Roles.CreatePersistentCookie)
cookie.Expires = rp.ExpireDate;
cookie.Secure = Roles.CookieRequireSSL;
context.Response.Cookies.Add(cookie);
}
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WebConfigManager.cs
- XPathNode.cs
- ErrorWebPart.cs
- ManualResetEventSlim.cs
- GenericPrincipal.cs
- WmlTextViewAdapter.cs
- Storyboard.cs
- GridViewUpdateEventArgs.cs
- RuntimeHelpers.cs
- DataComponentNameHandler.cs
- HelpProvider.cs
- SQLInt32Storage.cs
- Hashtable.cs
- XmlSchemaObjectTable.cs
- UnauthorizedWebPart.cs
- exports.cs
- FixedHyperLink.cs
- SimpleRecyclingCache.cs
- CommandID.cs
- ListenerAdapter.cs
- ItemCheckedEvent.cs
- IxmlLineInfo.cs
- ModelItemCollection.cs
- XmlHierarchyData.cs
- ExpressionParser.cs
- HandleCollector.cs
- DataRow.cs
- SqlHelper.cs
- SystemInformation.cs
- TextServicesContext.cs
- Root.cs
- UdpReplyToBehavior.cs
- FullTextBreakpoint.cs
- MemberAssignment.cs
- AbstractDataSvcMapFileLoader.cs
- IpcManager.cs
- BasicHttpMessageSecurityElement.cs
- PropertyEntry.cs
- WeakReferenceList.cs
- ReflectionServiceProvider.cs
- HostExecutionContextManager.cs
- FeatureSupport.cs
- MultiView.cs
- CatalogZoneBase.cs
- WebPartVerbCollection.cs
- SqlDataSourceCache.cs
- PeerCredential.cs
- UInt64Converter.cs
- NamespaceTable.cs
- XmlValueConverter.cs
- OrderedDictionaryStateHelper.cs
- IndentedWriter.cs
- OleDbConnectionInternal.cs
- ICspAsymmetricAlgorithm.cs
- ServerValidateEventArgs.cs
- XmlAttributeCollection.cs
- SHA384.cs
- FileDialogCustomPlace.cs
- DeviceSpecificChoiceCollection.cs
- OleDbWrapper.cs
- EdmRelationshipRoleAttribute.cs
- AuthenticationModuleElement.cs
- Attribute.cs
- DescendantBaseQuery.cs
- ConsumerConnectionPointCollection.cs
- FileNotFoundException.cs
- ExtendedProtectionPolicyTypeConverter.cs
- OdbcConnectionStringbuilder.cs
- InternalResources.cs
- X509Utils.cs
- RouteItem.cs
- TemplateNameScope.cs
- SetterTriggerConditionValueConverter.cs
- XmlReturnWriter.cs
- SqlUDTStorage.cs
- QualifiedCellIdBoolean.cs
- DesignerListAdapter.cs
- InternalConfigEventArgs.cs
- WebPartChrome.cs
- OutputScopeManager.cs
- TypeUtils.cs
- Helper.cs
- XamlParser.cs
- RecognitionEventArgs.cs
- RowUpdatedEventArgs.cs
- EntityContainer.cs
- XmlWriterSettings.cs
- ContextStack.cs
- JapaneseCalendar.cs
- EntityStoreSchemaFilterEntry.cs
- JournalEntryListConverter.cs
- ModelServiceImpl.cs
- DeadCharTextComposition.cs
- DesignerActionListCollection.cs
- X509SecurityTokenProvider.cs
- ToolStripPanelRow.cs
- URIFormatException.cs
- DeliveryStrategy.cs
- EventLogPermission.cs
- LeaseManager.cs