WCF "Basic" transport security issue when hosted in IIS

Phil Murray

I am attempting to secure a new .Net 4.5 WCF service using HTTPS / SSL, Basic client credentials and the WebHttpBinding. From reading up online I found a good series of Blog Posts from Allen Conway which I have used as a template.

WCF configuration

 <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webInteropSecureBinding" allowCookies="false" maxBufferPoolSize="2097152" maxBufferSize="2097152" maxReceivedMessageSize="2097152">
          <security mode="Transport">
            <transport clientCredentialType="Basic"></transport>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="PsmDataProvider.PsmProvider" behaviorConfiguration="SecureRest">
        <clear />
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="webInteropSecureBinding" name="PsmProvider" contract="PsmDataProvider.IPsmProvider" behaviorConfiguration="webHttpBehavior" />
        <endpoint address="mex" binding="mexHttpsBinding" name="mex" contract="IMetadataExchange" listenUriMode="Explicit" />
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:44300/PsmProvider/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="SecureRest">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" 
                                    customUserNamePasswordValidatorType="PsmDataProvider.Security.CustomerUserNamePasswordValidator, PsmDataProvider"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttpBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

CustomerUserNamePasswordValidator

I have stubbed out the CustomerUserNamePasswordValidator implementation and have confirmed that the constructor is called before the exception is raised.

using System;
using System.IdentityModel.Selectors;

namespace PsmDataProvider.Security
{
    internal class CustomerUserNamePasswordValidator : UserNamePasswordValidator, ICustomerUserNamePasswordValidator 
    {

        public CustomerUserNamePasswordValidator()
        {
        }

        public override void Validate(string userName, string password)
        {          
            if (userName == null) throw new ArgumentNullException("userName","The username must be provided in the request to access this service");
            if (password == null) throw new ArgumentNullException("password", "The password must be provided in the request to access this service");

        }
    }
}

When I try to run the code in VS2012 through IIS Express the service fails to start with the below error.

enter image description here

If I remove the clientCredentialType from the configuration then it works but I require the additional security of using the username / password validation on the service and possibly at a method level in the future.

Is this something I have configured incorrectly in the WCF config or a problem with the configuration in IISExpress?

Please help...

Phil Murray

The issue appears to be when using Basic Authentication when hosting the service in IIS as IIS wants to handle the authentication.

This is discussed in this MSDN blog post

In the version of WCF that shipped with .Net Framework 3.0 we didn't support custom validators with transport level HTTP security. We received much feedback from the community that this was a highly desired feature, so I'm happy to say we added support for this scenario in the 3.5 release of the .Net Framework. Note that this is only supported under self hosted services.

There is a resolution as discussed in Allen Conway's Blog Post by implementing a custom authorisation manager derived from ServiceAuthorizationManager

CustomAuthorizationManager

public class CustomAuthorizationManager : ServiceAuthorizationManager 
{
    private const string UserName = "username";
    private const string Password = "password";

    protected override bool CheckAccessCore(OperationContext operationContext)
    {
        string authHeader = WebOperationContext.Current.IncomingRequest.Headers["Authorization"];

        if ((authHeader != null) && (authHeader != string.Empty))
        {
            string[] svcCredentials = System.Text.ASCIIEncoding.ASCII
                                        .GetString(Convert.FromBase64String(authHeader.Substring(6)))
                                        .Split(':');

            var user = new { Name = svcCredentials[0], Password = svcCredentials[1] };

            if ((user.Name.Equals(UserName) && user.Password.Equals(Password)))
                return true;
            else
                return false;
        }
        else
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Add("WWW-Authenticate: Basic realm=\"PsmProvider\"");
            throw new WebFaultException(HttpStatusCode.Unauthorized);
        }
    }

}

Config

  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webInteropSecureBinding" allowCookies="false" maxBufferPoolSize="51200" maxBufferSize="51200" maxReceivedMessageSize="51200">
          <security mode="Transport"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="PsmDataProvider.PsmProvider" behaviorConfiguration="SecureRest">
        <clear />
        <endpoint binding="webHttpBinding" bindingConfiguration="webInteropSecureBinding" 
                    name="PsmProvider" contract="PsmDataProvider.IPsmProvider" behaviorConfiguration="webHttpBehavior" />
        <endpoint address="mex" binding="mexHttpsBinding" name="mex" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:44300/PsmProvider/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="SecureRest">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceAuthorization serviceAuthorizationManagerType="PsmDataProvider.Security.CustomAuthorizationManager, PsmDataProvider"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttpBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Note

Also note a comment from Travich regarding the IIS / IIS Express configuration

Travich said... One thing to help other users. It was briefly stated, but something I overlooked... Turn off Basic Auth in IIS and remove tag from your webHttpBinding!

Works for me.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

WCF hosted with IIS performance Tuning

分類Dev

Unable to consume WCF service (hosted in IIS )

分類Dev

Conflict when running IIS and WCF application listening on the same port (443)

分類Dev

Self-Hosted WCF - Namespace Reservation required?

分類Dev

issue recognising DataContract in WCF

分類Dev

openshiftでのHTTPStrict Transport Security

分類Dev

Self-hosted WCF service works with HTTP not with HTTPS

分類Dev

WCF Service not Starting Using Browse in IIS

分類Dev

Basic HTACCESS Issue

分類Dev

Basic CSS styling issue

分類Dev

.Net core application failing to upload via FTP, hosted on IIS

分類Dev

App Transport Security Xcode7ベータ6

分類Dev

Mandatory App Transport Security January 2017 impact of existing apps

分類Dev

WCF hosted in WPF and how can i change control in MainWindow UI from wcf?

分類Dev

WCF Server-to-server security mechanism

分類Dev

Facebook Javascript SDK security issue?

分類Dev

Is it impossible to have both transport level security and message level security in rampart? Why?

分類Dev

IdentityServer MVC throws OpenIdConnectProtocolException when hosted in Azure

分類Dev

React basic api call issue

分類Dev

Issue with a basic elasticsearch "terms" query

分類Dev

Excel Visual Basic Code Issue

分類Dev

Connecting IIS deployed WCF service with Azure service bus

分類Dev

WCFはIISでnetTcpBindingを使用します

分類Dev

Flask server visible when hosted on macbook, but not when hosted on windows desktop? (Tried everything?)

分類Dev

IIS Manager Error - Unable to bind to the underlying transport for [::]:80.The process cannot access the file because

分類Dev

Connect a self-hosted WCF-Service via WebHttpBinding fails in Kerberos-Mode

分類Dev

REST API basic security based on time

分類Dev

WCF error processing the token received from a Security Token Service

分類Dev

How to warmup an asp.net core app hosted in IIS (a) on website restart and (b) on redeploy?

Related 関連記事

  1. 1

    WCF hosted with IIS performance Tuning

  2. 2

    Unable to consume WCF service (hosted in IIS )

  3. 3

    Conflict when running IIS and WCF application listening on the same port (443)

  4. 4

    Self-Hosted WCF - Namespace Reservation required?

  5. 5

    issue recognising DataContract in WCF

  6. 6

    openshiftでのHTTPStrict Transport Security

  7. 7

    Self-hosted WCF service works with HTTP not with HTTPS

  8. 8

    WCF Service not Starting Using Browse in IIS

  9. 9

    Basic HTACCESS Issue

  10. 10

    Basic CSS styling issue

  11. 11

    .Net core application failing to upload via FTP, hosted on IIS

  12. 12

    App Transport Security Xcode7ベータ6

  13. 13

    Mandatory App Transport Security January 2017 impact of existing apps

  14. 14

    WCF hosted in WPF and how can i change control in MainWindow UI from wcf?

  15. 15

    WCF Server-to-server security mechanism

  16. 16

    Facebook Javascript SDK security issue?

  17. 17

    Is it impossible to have both transport level security and message level security in rampart? Why?

  18. 18

    IdentityServer MVC throws OpenIdConnectProtocolException when hosted in Azure

  19. 19

    React basic api call issue

  20. 20

    Issue with a basic elasticsearch "terms" query

  21. 21

    Excel Visual Basic Code Issue

  22. 22

    Connecting IIS deployed WCF service with Azure service bus

  23. 23

    WCFはIISでnetTcpBindingを使用します

  24. 24

    Flask server visible when hosted on macbook, but not when hosted on windows desktop? (Tried everything?)

  25. 25

    IIS Manager Error - Unable to bind to the underlying transport for [::]:80.The process cannot access the file because

  26. 26

    Connect a self-hosted WCF-Service via WebHttpBinding fails in Kerberos-Mode

  27. 27

    REST API basic security based on time

  28. 28

    WCF error processing the token received from a Security Token Service

  29. 29

    How to warmup an asp.net core app hosted in IIS (a) on website restart and (b) on redeploy?

ホットタグ

アーカイブ