1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
//----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
namespace System.ServiceModel.Description
{
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IdentityModel.Policy;
using System.Runtime.CompilerServices;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Web.Security;
public sealed class ServiceAuthorizationBehavior : IServiceBehavior
{
internal const bool DefaultImpersonateCallerForAllOperations = false;
internal const bool DefaultImpersonateOnSerializingReply = false;
internal const PrincipalPermissionMode DefaultPrincipalPermissionMode = PrincipalPermissionMode.UseWindowsGroups;
bool impersonateCallerForAllOperations;
bool impersonateOnSerializingReply;
ReadOnlyCollection<IAuthorizationPolicy> externalAuthorizationPolicies;
ServiceAuthorizationManager serviceAuthorizationManager;
PrincipalPermissionMode principalPermissionMode;
object roleProvider;
bool isExternalPoliciesSet;
bool isAuthorizationManagerSet;
bool isReadOnly;
public ServiceAuthorizationBehavior()
{
this.impersonateCallerForAllOperations = DefaultImpersonateCallerForAllOperations;
this.impersonateOnSerializingReply = DefaultImpersonateOnSerializingReply;
this.principalPermissionMode = DefaultPrincipalPermissionMode;
}
ServiceAuthorizationBehavior(ServiceAuthorizationBehavior other)
{
this.impersonateCallerForAllOperations = other.impersonateCallerForAllOperations;
this.impersonateOnSerializingReply = other.impersonateOnSerializingReply;
this.principalPermissionMode = other.principalPermissionMode;
this.roleProvider = other.roleProvider;
this.isExternalPoliciesSet = other.isExternalPoliciesSet;
this.isAuthorizationManagerSet = other.isAuthorizationManagerSet;
if (other.isExternalPoliciesSet || other.isAuthorizationManagerSet)
{
CopyAuthorizationPoliciesAndManager(other);
}
this.isReadOnly = other.isReadOnly;
}
public ReadOnlyCollection<IAuthorizationPolicy> ExternalAuthorizationPolicies
{
get
{
return this.externalAuthorizationPolicies;
}
set
{
ThrowIfImmutable();
this.isExternalPoliciesSet = true;
this.externalAuthorizationPolicies = value;
}
}
public bool ShouldSerializeExternalAuthorizationPolicies()
{
return this.isExternalPoliciesSet;
}
public ServiceAuthorizationManager ServiceAuthorizationManager
{
get
{
return this.serviceAuthorizationManager;
}
set
{
ThrowIfImmutable();
this.isAuthorizationManagerSet = true;
this.serviceAuthorizationManager = value;
}
}
public bool ShouldSerializeServiceAuthorizationManager()
{
return this.isAuthorizationManagerSet;
}
[DefaultValue(DefaultPrincipalPermissionMode)]
public PrincipalPermissionMode PrincipalPermissionMode
{
get
{
return this.principalPermissionMode;
}
set
{
if (!PrincipalPermissionModeHelper.IsDefined(value))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
}
ThrowIfImmutable();
this.principalPermissionMode = value;
}
}
[DefaultValue(null)]
public RoleProvider RoleProvider
{
get
{
return (RoleProvider)this.roleProvider;
}
set
{
ThrowIfImmutable();
this.roleProvider = value;
}
}
[DefaultValue(DefaultImpersonateCallerForAllOperations)]
public bool ImpersonateCallerForAllOperations
{
get
{
return this.impersonateCallerForAllOperations;
}
set
{
ThrowIfImmutable();
this.impersonateCallerForAllOperations = value;
}
}
[DefaultValue(DefaultImpersonateOnSerializingReply)]
public bool ImpersonateOnSerializingReply
{
get
{
return this.impersonateOnSerializingReply;
}
set
{
ThrowIfImmutable();
this.impersonateOnSerializingReply = value;
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
void ApplyAuthorizationPoliciesAndManager(DispatchRuntime behavior)
{
if (this.externalAuthorizationPolicies != null)
{
behavior.ExternalAuthorizationPolicies = this.externalAuthorizationPolicies;
}
if (this.serviceAuthorizationManager != null)
{
behavior.ServiceAuthorizationManager = this.serviceAuthorizationManager;
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
void CopyAuthorizationPoliciesAndManager(ServiceAuthorizationBehavior other)
{
this.externalAuthorizationPolicies = other.externalAuthorizationPolicies;
this.serviceAuthorizationManager = other.serviceAuthorizationManager;
}
[MethodImpl(MethodImplOptions.NoInlining)]
void ApplyRoleProvider(DispatchRuntime dispatchRuntime)
{
dispatchRuntime.RoleProvider = (RoleProvider)this.roleProvider;
}
void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
{
}
void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
{
}
void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
{
if (description == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("description"));
}
if (serviceHostBase == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("serviceHostBase"));
}
for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
{
ChannelDispatcher channelDispatcher = serviceHostBase.ChannelDispatchers[i] as ChannelDispatcher;
if (channelDispatcher != null && !ServiceMetadataBehavior.IsHttpGetMetadataDispatcher(description, channelDispatcher))
{
foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
{
DispatchRuntime behavior = endpointDispatcher.DispatchRuntime;
behavior.PrincipalPermissionMode = this.principalPermissionMode;
if (!endpointDispatcher.IsSystemEndpoint)
{
behavior.ImpersonateCallerForAllOperations = this.impersonateCallerForAllOperations;
behavior.ImpersonateOnSerializingReply = this.impersonateOnSerializingReply;
}
if (this.roleProvider != null)
{
ApplyRoleProvider(behavior);
}
if (this.isAuthorizationManagerSet || this.isExternalPoliciesSet)
{
ApplyAuthorizationPoliciesAndManager(behavior);
}
}
}
}
}
internal ServiceAuthorizationBehavior Clone()
{
return new ServiceAuthorizationBehavior(this);
}
internal void MakeReadOnly()
{
this.isReadOnly = true;
}
void ThrowIfImmutable()
{
if (this.isReadOnly)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
}
}
}
}
|