Prithvi Ramana
Prithvi Ramana

Reputation: 241

How to make a WCF Service work with https?

I am trying to enable https in my WCF service. I've added the SSL certificate to my localhost and I am able to view the WSDL when I use https in the browser. And the WCF service works fine with http without the SSL configuration in the web.config. But as soon as I change it to use https, I get the following error. If anybody can point me to the solution for this issue, it will be helpful as there isn't much reference for this error.

at Microsoft.Http.HttpStageProcessingAsyncResult.Complete(HttpStage stage, Exception e)
at Microsoft.Http.HttpStageProcessingAsyncResult.NextRequest(HttpStageProcessingAsyncResult self)
at Microsoft.Http.HttpStageProcessingAsyncResult..ctor(HttpStageProcessingAsyncState state, AsyncCallback callback, Object user)
at Microsoft.Http.HttpClient.Send(HttpRequestMessage request)
at Microsoft.Http.HttpClient.Send(HttpMethod method, Uri uri, RequestHeaders headers, HttpContent content)
at Microsoft.Http.HttpClient.Send(HttpMethod method, Uri uri)
at Microsoft.Http.HttpMethodExtensions.Method(HttpClient client, HttpMethod method, Uri uri)
at Microsoft.Http.HttpMethodExtensions.Method(HttpClient client, HttpMethod method, String uri)
at Microsoft.Http.HttpMethodExtensions.Get(HttpClient client, String uri)
at MobileScreening.Client.CommonTests.LoginPost(HttpClient client, String username, String password) in c:\TFS Projects\MobileScreening\MobileScreening.Client\CommonTests.cs:line 118

My client code where I am making the request to the service

static string LoginPost(HttpClient client, string username, string password)
    {
        string key = string.Empty;

        try
        {
            var user = new UserCredentials
            {
                Email = username,
                Password = password
            };

            Console.WriteLine("User Authentication:");

            HttpContent content = HttpContentExtensions.CreateJsonDataContract(user);

            using (HttpResponseMessage response = client.Post("AuthenticationService.svc/", content))
            {
                Console.WriteLine(response.Content.ReadAsString());
                Console.WriteLine(response.Headers.ToString());

                key = response.Headers["MobileScreening"] ?? string.Empty;
            }
        }
        catch (Exception ex)
        {
            var stack = ex.StackTrace;
            var innerException = ex.InnerException;
            var message = ex.Message;
        }

        return key;
    }

My interface with the operation contract

public interface IAuthenticationService
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    AuthenticationWrapper Authenticate(UserCredentials user);
}

My Service to authenticate users:

public class AuthenticationService : IAuthenticationService
{
    public AuthenticationWrapper Authenticate(UserCredentials user)
    {
        string email = user.Email ?? string.Empty;
        string password = user.Password ?? string.Empty;

        var authentication = new Authentication();

        var authenticationWrapper = new AuthenticationWrapper();

        if (!authentication.AuthenticateUser(email, password))
        {
            const string description = "Authentication failed. Username and/or password is incorrect.";

            BLL.Authentication.ThrowAuthorisationFailed(description, email);

            WebOperationContext ctx = WebOperationContext.Current;
            ctx.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized;

            authenticationWrapper.Code = (short)HttpStatusCode.Unauthorized;
            authenticationWrapper.Status = HttpStatusCode.Unauthorized.ToString();
            authenticationWrapper.Message = description;
            return authenticationWrapper;
        }
        else
        {
            const string description = "Authentication: Authenticate User";

            LogHandler.LogMessage(email, description, Common.Event.LoginSuccessful);

            string authorisationKey = authentication.CreateAuthorisationKey(email);

            WebOperationContext ctx = WebOperationContext.Current;
            ctx.OutgoingResponse.Headers.Add(Common.AuthorisationHeader, authorisationKey);
            ctx.OutgoingResponse.StatusCode = HttpStatusCode.OK;

            authenticationWrapper.Code = (short)HttpStatusCode.OK;
            authenticationWrapper.Status = HttpStatusCode.OK.ToString();
            authenticationWrapper.Message = description;
            return authenticationWrapper;
        }
    }
}

Finally my web.config configuration for the service

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />
<services>
  <service name="MobileScreening.ServiceApp.AuthenticationService">
    <endpoint address="AuthenticationService" binding="basicHttpBinding" bindingConfiguration="secureHttpBinding" contract="MobileScreening.ServiceApp.IAuthenticationService" />
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
  </service>
  <service name="MobileScreening.ServiceApp.ProgrammeService">
    <endpoint address="ProgrammeService" binding="basicHttpBinding" bindingConfiguration="secureHttpBinding" contract="MobileScreening.ServiceApp.IProgrammeService" />
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
  </service>
  <service name="MobileScreening.ServiceApp.ActivityService">
    <endpoint address="ActivityService" binding="basicHttpBinding" bindingConfiguration="secureHttpBinding" contract="MobileScreening.ServiceApp.IActivityService" />
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
  </service>
  <service name="MobileScreening.ServiceApp.UserConfigurationService">
    <endpoint address="UserConfigurationService" binding="basicHttpBinding" bindingConfiguration="secureHttpBinding" contract="MobileScreening.ServiceApp.IUserConfigurationService" />
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
  </service>
</services>
<bindings>
  <basicHttpBinding>
    <binding name="secureHttpBinding">
      <security mode="Transport">
        <transport clientCredentialType="Basic"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpsGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

Upvotes: 1

Views: 1375

Answers (1)

Gui
Gui

Reputation: 296

Create a new self signed certificate using your machine name instead of the local host. Configure your solution to use the new certificate and make sure that it is installed in the client machine's under the Trusted Root Certification Authority (in your case, the client is also the server).

Also, you may also try to create the certificate pointing to the machine's fully qualified domain name.

Upvotes: 1

Related Questions