0

我有一个使用 basicHttpBinding 的 WCF 服务。现在我想保护我的方法,这样没有人可以创建代理并使用我的方法。我使用了来自 msdn 的 WCF 客户端认证。却不能更进一步。这是我的 web.config

 <system.serviceModel>
<client>
  <endpoint address="http://localhost:57246/Service1.svc" binding="basicHttpBinding"
    bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
    name="BasicHttpBinding_IService1" />
</client>
<services>
  <service name="Microsoft.ServiceModel.Samples.CalculatorService"
           behaviorConfiguration="DataServiceBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:57246/Service1.svc"/>
      </baseAddresses>
    </host>
    <endpoint address=""
       binding="basicHttpBinding"
       bindingConfiguration="Binding1"
       contract="ServiceReference1.IService1" />

    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>

<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IService1">
      <security mode="Message">
        <transport realm="" />
        <message clientCredentialType="Certificate" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>


<behaviors>
  <serviceBehaviors>
    <behavior name="DataServiceBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="False" />

      <serviceCredentials>
        <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
        <clientCertificate>

          <authentication certificateValidationMode="PeerOrChainTrust" />
        </clientCertificate>
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>

现在应该通过升c调用什么。我在 WCF 中有一个名为 showGrid 的方法。

public DataSet showGrid()
    {
        SqlDataAdapter da = new SqlDataAdapter("Select * FROM Resources", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }

并尝试在单击按钮时这样调用它

protected void btnShow_Click(object sender, EventArgs e)
    {
        var client = new ServiceReference1.Service1Client();


 client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "localhost");
        client.showGrid();
        GridView1.DataSource = client.showGrid();
        GridView1.DataBind();
    }

现在一个异常是这样抛出的

无法使用以下搜索条件找到 X.509 证书:StoreName 'My'、StoreLocation 'CurrentUser'、FindType 'FindBySubjectName'、FindValue 'localhost'。

如何克服这一点。请帮我。我也用谷歌搜索它。我正在使用 Windows 7 这是保护我的方法的正确方法吗?请指导我。找了很多天这个问题。

4

1 回答 1

1

只需像这样添加客户端证书

client.ClientCredentials.ClientCertificate.Certificate = yourcert;

编辑:

        X509Certificate2 yourcert= null; 
        var store = new X509Store(storeName, storeLocation);

        store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
        var certCollection = store.Certificates.Find(findType, thumbprint, false);
        if (certCollection.Count>0)
            yourcert= certCollection[0];
        store.Close();
于 2012-11-17T10:12:52.927 回答