繁体   English   中英

如何使WCF服务与https一起使用?

[英]How to make a WCF Service work with https?

我正在尝试在WCF服务中启用https。 我已将SSL证书添加到本地主机,并且在浏览器中使用https时可以查看WSDL。 而且WCF服务可以与http正常运行,而无需在web.config中进行SSL配置。 但是,一旦我将其更改为使用https,就会收到以下错误。 如果有人可以指出该问题的解决方案,这将很有帮助,因为该错误的参考文献很少。

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

我向服务提出要求的客户代码

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;
    }

我的操作合同界面

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

我的服务以验证用户身份:

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;
        }
    }
}

最后,我对服务的web.config配置

<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>

使用您的计算机名称而不是本地主机创建一个新的自签名证书。 配置解决方案以使用新证书,并确保已将其安装在受信任的根证书颁发机构下的客户端计算机中(在您的情况下,客户端也是服务器)。

另外,您也可以尝试创建指向计算机的完全合格域名的证书。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM