Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Extensions / ApplicationServices / RoleService.cs / 1305376 / RoleService.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.ApplicationServices {
using System;
using System.Diagnostics.CodeAnalysis;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Configuration;
using System.Runtime.Serialization;
using System.Web;
using System.Web.Security;
using System.Web.Configuration;
using System.Web.Management;
using System.Web.Resources;
using System.Security.Principal;
using System.Web.Hosting;
using System.Threading;
using System.Configuration.Provider;
[
AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required),
ServiceContract(Namespace = "http://asp.net/ApplicationServices/v200"),
ServiceBehavior(Namespace="http://asp.net/ApplicationServices/v200", InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)
]
public class RoleService {
private static object _selectingProviderEventHandlerLock = new object();
private static EventHandler _selectingProvider;
public static event EventHandler SelectingProvider {
add {
lock (_selectingProviderEventHandlerLock) {
_selectingProvider += value;
}
}
remove {
lock (_selectingProviderEventHandlerLock) {
_selectingProvider -= value;
}
}
}
private static void EnsureProviderEnabled() {
if (!Roles.Enabled) {
throw new ProviderException(AtlasWeb.RoleService_RolesFeatureNotEnabled);
}
}
private RoleProvider GetRoleProvider(IPrincipal user) {
string providerName = Roles.Provider.Name;
SelectingProviderEventArgs args = new SelectingProviderEventArgs(user, providerName);
OnSelectingProvider(args);
providerName = args.ProviderName;
RoleProvider provider = Roles.Providers[providerName];
if (provider == null) {
throw new ProviderException(AtlasWeb.RoleService_RoleProviderNotFound);
}
return provider;
}
[OperationContract]
public string[] GetRolesForCurrentUser() {
try {
ApplicationServiceHelper.EnsureRoleServiceEnabled();
EnsureProviderEnabled();
IPrincipal user = ApplicationServiceHelper.GetCurrentUser(HttpContext.Current);
string username = ApplicationServiceHelper.GetUserName(user);
RoleProvider provider = GetRoleProvider(user);
return provider.GetRolesForUser(username);
}
catch (Exception e) {
LogException(e);
throw;
}
}
[OperationContract]
public bool IsCurrentUserInRole(string role) {
if (role == null) {
throw new ArgumentNullException("role");
}
try {
ApplicationServiceHelper.EnsureRoleServiceEnabled();
EnsureProviderEnabled();
IPrincipal user = ApplicationServiceHelper.GetCurrentUser(HttpContext.Current);
string username = ApplicationServiceHelper.GetUserName(user);
RoleProvider provider = GetRoleProvider(user);
return provider.IsUserInRole(username, role);
}
catch (Exception e) {
LogException(e);
throw;
}
}
private void LogException(Exception e) {
WebServiceErrorEvent errorevent = new WebServiceErrorEvent(AtlasWeb.UnhandledExceptionEventLogMessage, this, e);
errorevent.Raise();
}
private void OnSelectingProvider(SelectingProviderEventArgs e) {
EventHandler handler = _selectingProvider;
if (handler != null) {
handler(this, e);
}
}
//hiding public constructor
internal RoleService() {
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.ApplicationServices {
using System;
using System.Diagnostics.CodeAnalysis;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Configuration;
using System.Runtime.Serialization;
using System.Web;
using System.Web.Security;
using System.Web.Configuration;
using System.Web.Management;
using System.Web.Resources;
using System.Security.Principal;
using System.Web.Hosting;
using System.Threading;
using System.Configuration.Provider;
[
AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required),
ServiceContract(Namespace = "http://asp.net/ApplicationServices/v200"),
ServiceBehavior(Namespace="http://asp.net/ApplicationServices/v200", InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)
]
public class RoleService {
private static object _selectingProviderEventHandlerLock = new object();
private static EventHandler _selectingProvider;
public static event EventHandler SelectingProvider {
add {
lock (_selectingProviderEventHandlerLock) {
_selectingProvider += value;
}
}
remove {
lock (_selectingProviderEventHandlerLock) {
_selectingProvider -= value;
}
}
}
private static void EnsureProviderEnabled() {
if (!Roles.Enabled) {
throw new ProviderException(AtlasWeb.RoleService_RolesFeatureNotEnabled);
}
}
private RoleProvider GetRoleProvider(IPrincipal user) {
string providerName = Roles.Provider.Name;
SelectingProviderEventArgs args = new SelectingProviderEventArgs(user, providerName);
OnSelectingProvider(args);
providerName = args.ProviderName;
RoleProvider provider = Roles.Providers[providerName];
if (provider == null) {
throw new ProviderException(AtlasWeb.RoleService_RoleProviderNotFound);
}
return provider;
}
[OperationContract]
public string[] GetRolesForCurrentUser() {
try {
ApplicationServiceHelper.EnsureRoleServiceEnabled();
EnsureProviderEnabled();
IPrincipal user = ApplicationServiceHelper.GetCurrentUser(HttpContext.Current);
string username = ApplicationServiceHelper.GetUserName(user);
RoleProvider provider = GetRoleProvider(user);
return provider.GetRolesForUser(username);
}
catch (Exception e) {
LogException(e);
throw;
}
}
[OperationContract]
public bool IsCurrentUserInRole(string role) {
if (role == null) {
throw new ArgumentNullException("role");
}
try {
ApplicationServiceHelper.EnsureRoleServiceEnabled();
EnsureProviderEnabled();
IPrincipal user = ApplicationServiceHelper.GetCurrentUser(HttpContext.Current);
string username = ApplicationServiceHelper.GetUserName(user);
RoleProvider provider = GetRoleProvider(user);
return provider.IsUserInRole(username, role);
}
catch (Exception e) {
LogException(e);
throw;
}
}
private void LogException(Exception e) {
WebServiceErrorEvent errorevent = new WebServiceErrorEvent(AtlasWeb.UnhandledExceptionEventLogMessage, this, e);
errorevent.Raise();
}
private void OnSelectingProvider(SelectingProviderEventArgs e) {
EventHandler handler = _selectingProvider;
if (handler != null) {
handler(this, e);
}
}
//hiding public constructor
internal RoleService() {
}
}
}
// 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
- SpStreamWrapper.cs
- SecurityResources.cs
- InputReport.cs
- HtmlEncodedRawTextWriter.cs
- StaticSiteMapProvider.cs
- IdentityVerifier.cs
- Point3DConverter.cs
- Point3DAnimation.cs
- ConfigErrorGlyph.cs
- PackageRelationshipCollection.cs
- EntityDataSourceQueryBuilder.cs
- ActivityStateQuery.cs
- XmlDataContract.cs
- HttpListenerResponse.cs
- ErrorActivity.cs
- DataBindEngine.cs
- KeyFrames.cs
- OleDbParameter.cs
- HtmlInputSubmit.cs
- EllipseGeometry.cs
- MonthChangedEventArgs.cs
- TrackingConditionCollection.cs
- FixedSOMGroup.cs
- VScrollBar.cs
- RegexRunnerFactory.cs
- SqlCommandBuilder.cs
- SamlAttributeStatement.cs
- QueryCursorEventArgs.cs
- OptimisticConcurrencyException.cs
- OutOfProcStateClientManager.cs
- prefixendpointaddressmessagefiltertable.cs
- ComNativeDescriptor.cs
- EntityConnection.cs
- StylusPoint.cs
- KeyedCollection.cs
- ControlUtil.cs
- _ShellExpression.cs
- RedistVersionInfo.cs
- ContextStaticAttribute.cs
- IgnoreFlushAndCloseStream.cs
- UIElementParaClient.cs
- PTProvider.cs
- EndOfStreamException.cs
- HttpApplicationFactory.cs
- cache.cs
- Image.cs
- MetadataArtifactLoaderResource.cs
- KeyNotFoundException.cs
- OrderByQueryOptionExpression.cs
- FormViewDeleteEventArgs.cs
- _Win32.cs
- _DigestClient.cs
- WebEventTraceProvider.cs
- IUnknownConstantAttribute.cs
- ZoneMembershipCondition.cs
- DataTableClearEvent.cs
- TemplateContentLoader.cs
- XPathDescendantIterator.cs
- ListenerConstants.cs
- GlobalProxySelection.cs
- HandlerMappingMemo.cs
- Base64Encoding.cs
- XmlNode.cs
- Section.cs
- AssociationProvider.cs
- _DisconnectOverlappedAsyncResult.cs
- ConfigurationValidatorAttribute.cs
- HistoryEventArgs.cs
- RecipientServiceModelSecurityTokenRequirement.cs
- GroupDescription.cs
- MessageQueuePermissionEntryCollection.cs
- SaveFileDialog.cs
- CertificateManager.cs
- HwndSubclass.cs
- Brush.cs
- HttpApplicationStateBase.cs
- BitmapPalette.cs
- xamlnodes.cs
- CapabilitiesAssignment.cs
- TreeViewCancelEvent.cs
- XmlSchemaException.cs
- WebBrowserProgressChangedEventHandler.cs
- NodeLabelEditEvent.cs
- StructuralType.cs
- ProtocolsConfigurationHandler.cs
- DispatchWrapper.cs
- DescendantOverDescendantQuery.cs
- Privilege.cs
- ActivityCollectionMarkupSerializer.cs
- MaskedTextBoxDesignerActionList.cs
- UIntPtr.cs
- HMACSHA512.cs
- DateTimeConstantAttribute.cs
- MachineKeySection.cs
- GeometryGroup.cs
- PathGeometry.cs
- Marshal.cs
- PropertyValueUIItem.cs
- XamlParser.cs
- RepeatBehaviorConverter.cs