我有一个使用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 sharp。我在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'InfurrentUser',FindType'FindBySubjectName',FindValue'localhost'。
如何克服这一点。请帮我。我也谷歌。我正在使用Windows 7这是保护我的方法的正确方法吗?请指导我。多天来一直在寻找这个问题。
答案 0 :(得分: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();