简体   繁体   English

使用自签名证书的WCF服务

[英]consuming WCF service with self-signed certificate

When I execute the consuming application it is giving me the below exception: The requested service, ' https://localhost:53996/HistoricStatementsWS.HistoricStatements.svc ' could not be activated. 执行消费应用程序时,它给了我以下异常:无法激活请求的服务“ https:// localhost:53996 / HistoricStatementsWS.HistoricStatements.svc ”。

and when I try to enter this path into chrome it says: A registration already exists for URI ' https://ws20.intra.local:53996/HistoricStatementsWS.HistoricStatements.svc '. 当我尝试在chrome中输入此路径时,它说:URI'https://ws20.intra.local:53996 / HistoricStatementsWS.HistoricStatements.svc '的注册已经存在。

I don't know how to get rid of these exceptions and I have been through a lot of forums so far. 我不知道如何摆脱这些例外,到目前为止,我已经浏览了很多论坛。

SERVER-SIDE app.config 服务器端app.config

<system.web>
<compilation debug="true" />
<membership defaultProvider="ClientAuthenticationMembershipProvider">
  <providers>
    <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
  </providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
  <providers>
    <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
  </providers>
</roleManager>
</system.web>
<system.serviceModel>
<bindings>
  <wsHttpBinding>
    <binding name="wsHttpEndpointBinding">
      <security mode="Transport">
        <transport clientCredentialType="Certificate" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="behaviourHttps" name="HistoricStatementsWS.HistoricStatements">
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding"
      name="wsHttpEndpoint" contract="HistoricStatementsWS.IHistoricStatements" />
    <endpoint address="HistoricStatementsWS.HistoricStatements.svc"
      binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding"
      name="mexEndpoint" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="https://localhost:53996/" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="behaviourHttps">
      <useRequestHeadersForMetadataAddress />
      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"
        httpsGetUrl="https://localhost:53996/HistoricStatementsWS.HistoricStatements.svc"
        policyVersion="Policy15" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

CLIENT-SIDE Webconfig 客户端Webconfig

<configuration>
<configSections>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<directoryBrowse enabled="true" showFlags="Date,Time,Extension,Size" />
</system.webServer>
<system.serviceModel>
<bindings>
  <wsHttpBinding>
    <binding name="wsHttpEndpoint">
      <security mode="Transport">
        <transport clientCredentialType="Certificate" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<client>
  <endpoint behaviorConfiguration="endpointBehavior" address="https://localhost:53996/HistoricStatementsWS.HistoricStatements.svc" binding="wsHttpBinding"
    bindingConfiguration="wsHttpEndpoint" contract="IHistoricStatements.IHistoricStatements"
    name="wsHttpEndpoint" />
</client>
<behaviors>
  <endpointBehaviors>
    <behavior name="endpointBehavior">
      <clientCredentials>
        <clientCertificate storeLocation="LocalMachine" storeName="My" findValue="00B192126A72D282D2" x509FindType="FindBySerialNumber"/>
        <serviceCertificate>
          <authentication certificateValidationMode="None" revocationMode="NoCheck" />
        </serviceCertificate>
      </clientCredentials>
    </behavior>
  </endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

For anyone with same problem I managed to solve the issue by directing my ip 127.0.0.1 to my full domain name: computername.intra.local. 对于任何有相同问题的人,我都设法通过将ip 127.0.0.1指向我的完整域名:computername.intra.local来解决了该问题。 I changed localhost in the server's web config to my domain name (computername.intra.local) and removed the domain prefix from httpsGetUrl since the baseAddress is used also for this value so https://localhost:53996/ was duplicated. 我将服务器的Web配置中的localhost更改为我的域名(computername.intra.local),并从httpsGetUrl中删除了域名前缀,因为baseAddress也用于此值,因此重复了https:// localhost:53996 / Although there is still duplicate values and the config is still not accurate, at least the wsdl is accessible from the browser. 尽管仍然存在重复的值,并且配置仍然不准确,但是至少可以从浏览器访问wsdl。 The browser (on my local) asks for a certificate and authenticates successfully. 浏览器(在我的本地计算机上)请求证书并成功进行身份验证。

However I still lack the know how of reaching the same url from a different machine on the same network. 但是,我仍然不知道如何从同一网络上的另一台计算机访问相同的URL。 I installed the root and client certificates as they are on my local machine and still it gives this error: 'The HTTP request was forbidden with client authentication scheme 'Anonymous'.' 我将根证书和客户端证书安装在本地计算机上,但仍然出现此错误:“ HTTP请求被客户端身份验证方案“匿名”禁止。” I was having this error on my local from the client side but solved it by calling the certificate programmatically. 我在客户端的本地出现此错误,但通过以编程方式调用证书解决了该错误。 The same code on the new machine does not work. 新计算机上的相同代码不起作用。

The code is: 代码是:

WSHttpBinding httpBinding = new WSHttpBinding(SecurityMode.Transport);
httpBinding.Security.Transport.ClientCredentialType =     HttpClientCredentialType.Certificate;
httpBinding.Security.Message.NegotiateServiceCredential = false;
httpBinding.Security.Message.EstablishSecurityContext = false;

var httpUri = new Uri("https://ws12.intra.local:53996/HistoricStatementsWS.Historicstatements.svc");
var httpEndpoint = new EndpointAddress(httpUri, EndpointIdentity.CreateDnsIdentity(""));
var newFactory = new ChannelFactory<IHistoricStatements>(httpBinding, httpEndpoint);
newFactory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "ws12.intra.local");
newFactory.Credentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "ws12.intra.local");

I must add that no proxies are used, 'Anonymous' is switched on in IIS with user IUSR, root folder has full permissios to IUSR, IIS_IUSRS, Network, Network Service. 我必须补充一点,不使用代理,在IIS中使用用户IUSR打开“匿名”,根文件夹具有对IUSR,IIS_IUSRS,网络,网络服务的完全许可。 I first wish to connect from the browser on new machine as this gives error: 我首先希望从新计算机上的浏览器进行连接,因为这会导致错误:

403 -Forbidden: Access is denied. 403-禁止访问:访问被拒绝。 You do not have permission to view this directory or page using the credentials that you supplied. 您无权使用您提供的凭据查看此目录或页面。

Your replies are much appreciated. 非常感谢您的答复。

Justin 贾斯汀

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

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