Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the Thread.CurrentPrincipal from IAuthorizationPolicy?

Tags:

c#

wcf

principal

I have a WCF service running under .NET Framework 4.6.2. I have used the web.config before to configure the service with my custom IAuthorizationPolicy like this :

<services>
behaviorConfiguration="MyClientService.CustomValidator_Behavior" name="My.Service.Implementation.Services.MyClientService">
        <endpoint binding="netHttpBinding" behaviorConfiguration="protoEndpointBehavior" address="BinaryHttpProto" bindingNamespace="http://My.ServiceContracts/2007/11" contract="My.ServiceContracts.IMyClientService" />
        <endpoint binding="netHttpsBinding" behaviorConfiguration="protoEndpointBehavior" address="BinaryHttpsProto" bindingNamespace="http://My.ServiceContracts/2007/11" contract="My.ServiceContracts.IMyClientService" />
        bindingConfiguration="netTcpCertificate" behaviorConfiguration="protoEndpointBehavior" bindingNamespace="http://My.ServiceContracts/2007/11" contract="My.ServiceContracts.IMyClientService" address="Sll"/>
        <host>
        </host>
      </service>
    </services>


    <behavior name="MyClientService.CustomValidator_Behavior">
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceMetadata httpGetEnabled="true" />
      <customBehaviorExtension_ClientService />
      <serviceThrottling maxConcurrentCalls="2000" maxConcurrentSessions="2147483647" maxConcurrentInstances="2000" />
      <serviceCredentials>
        <clientCertificate>
          <authentication certificateValidationMode="PeerOrChainTrust" />
        </clientCertificate>
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="My.Service.Implementation.Security.CustomUsernamePasswordValidator, My.Service.Implementation" />
      </serviceCredentials>
      <serviceAuthorization principalPermissionMode="Custom" serviceAuthorizationManagerType="My.Service.Implementation.Security.CustomServiceAuthorizationManager, My.Service.Implementation">
        <authorizationPolicies>
          <add policyType="My.Service.Implementation.Security.CustomAuthorizationPolicy_ClientService, My.Service.Implementation" />
        </authorizationPolicies>
      </serviceAuthorization>
    </behavior>

Now I need to swtich to do this in code and this is what that looks like :

var endpoint = new System.ServiceModel.Description.ServiceEndpoint(System.ServiceModel.Description.ContractDescription.GetContract(typeof(IMyClientService)),
                        binding,
                       new EndpointAddress(endPointAddress));

                endpoint.EndpointBehaviors.Add(new ProtoBuf.ServiceModel.ProtoEndpointBehavior());
                serviceHost.AddServiceEndpoint(endpoint);

                ServiceAuthorizationBehavior serviceAuthorizationBehavior = serviceHost.Description.Behaviors.Find<ServiceAuthorizationBehavior>();
                if (serviceAuthorizationBehavior == null)
                {
                    serviceAuthorizationBehavior = new ServiceAuthorizationBehavior();
                    serviceHost.Description.Behaviors.Add(serviceAuthorizationBehavior);
                }
                serviceAuthorizationBehavior.ExternalAuthorizationPolicies = new ReadOnlyCollection<IAuthorizationPolicy>(new IAuthorizationPolicy[] { new CustomAuthorizationPolicy_ClientService() });
                ((ServiceBehaviorAttribute)serviceHost.Description.Behaviors[typeof(ServiceBehaviorAttribute)]).MaxItemsInObjectGraph = 2147483647;
                ((ServiceBehaviorAttribute)serviceHost.Description.Behaviors[typeof(ServiceBehaviorAttribute)]).IncludeExceptionDetailInFaults = true;

                ServiceThrottlingBehavior throttleBehavior = new ServiceThrottlingBehavior
                {
                    MaxConcurrentCalls = 200,
                    MaxConcurrentInstances = 2147483647,
                    MaxConcurrentSessions = 2000,
                };
                serviceHost.Description.Behaviors.Add(throttleBehavior);

                Console.WriteLine("Starting service...");
                serviceHost.Open();
                Console.WriteLine("Service started successfully (" + uri + ")");
                return serviceHost;
            }
            catch(Exception ex)

In the IAuthorizationPolicy i set the principla like this just as before and it does break here :

var userContext = new UserContextOnService(new ClientIdentity { AuthenticationType = "regular", IsAuthenticated = true, Name = "username" }, currentAnvandare, LoginType.UsernamePassword);
            userContext.OrbitToken = orbitToken;
            evaluationContext.Properties["Principal"] = userContext;
            SharedContext.Instance.AddUserContext(person.PersonId.ToString(), userContext);

The problem is that when I try to run this :

(UserContextOnService)Thread.CurrentPrincipal;

In the service method I get exception, the CurrentPrincipal is a WindowPrincipal?

I can get the correct Principal by using this code :

OperationContext.Current.ServiceSecurityContext.AuthorizationContext.Properties["Principal"]

But that would mean to change in MANY places where the context is fetched with just Thread.CurrentPrincipal.

I suspect that I have lost something in the configuration?

Edit : Have tried to set the Thread.CurrentPrincipal = userContext; in the Evaluate method but this does not help, the Thread.CurrentPrincipal if still a WindowsPrinciple? I suspect that the servicemethod is ending up on another thread then the one that executes the Evaluate.

like image 264
Banshee Avatar asked Feb 28 '17 15:02

Banshee


1 Answers

When starting the service this needed to be set as well :

serviceAuthorizationBehavior.PrincipalPermissionMode = PrincipalPermissionMode.Custom;
serviceAuthorizationBehavior.ServiceAuthorizationManager = new CustomServiceAuthorizationManager();

This is done right above the following line :

serviceAuthorizationBehavior.ExternalAuthorizationPolicies = new ReadOnlyCollection<IAuthorizationPolicy>(new IAuthorizationPolicy[] { new CustomAuthorizationPolicy_ClientService() });
like image 167
Banshee Avatar answered Nov 09 '22 23:11

Banshee