首先,有很多相同标题的问题,但我相信我的问题与其他问题有点不同。我想说的是,大约两天前一切都很好。我没有更改任何代码。
我们不需要在我们的服务器或本地计算机上安装任何 SSL 证书。 WCF服务的url在浏览器上正常打开(https://sampleurl.com/service.svc)
我创建了一个控制台应用程序用于测试目的,它可以工作,但在我们的 ASP.NET MVC 5 项目中不起作用。
如何致电服务:
CustomerServiceClient client = new CustomerServiceClient();
client.ClientCredentials.UserName.UserName = "[email protected]";
client.ClientCredentials.UserName.Password = "asfasf";
var result = client.GetInformation("1MS0274796");
回复是:
System.ServiceModel.Security.SecurityNegotiationException:“无法使用权限‘servis.vatanbilgisayar.com’建立 SSL/TLS 安全通道。”
内部异常:WebException:请求已中止:无法创建 SSL/TLS 安全通道。
我尝试过的
我在服务调用之前添加了这行代码,但没有起作用:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
和
System.Net.ServicePointManager.ServerCertificateValidationCallback +=
(se, cert, chain, sslerror) =>
{
return true;
};
添加此代码会引发此错误:
不支持证书密钥算法
web.config
:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="CustomerServiceSoap">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="https://xyzx.com/CustomerService.svc"
binding="wsHttpBinding"
bindingConfiguration="CustomerServiceSoap"
contract="xfasf.asfasf" name="CustomerServiceSoap" />
</client>
</system.serviceModel>
您需要一个证书,并且需要生成一个验证类。我给你举个例子:
服务web.config:
?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1" />
</startup>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="Service1Binding">
<security mode="Message">
<transport clientCredentialType="None" />
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="Service1Behavior" name="ConsoleApp6.Service1">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="Service1Binding"
contract="ConsoleApp6.IService1">
<identity>
<dns value="QiYouCert" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="......" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceCredentials>
<serviceCertificate findValue="QiYouCert" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="TrustedPeople" />
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="ConsoleApp6.CustomUserNameValidator,ConsoleApp6" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
服务验证类:
public class CustomUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (null == userName || null == password)
{
throw new ArgumentNullException();
}
if (userName != "admin" && password != "wcf.admin")
{
throw new System.IdentityModel.Tokens.SecurityTokenException("Unknown Username or Password");
}
}
}
客户账户及密码验证:
using (var proxy = new ServiceReference1.Service1Client())
{
proxy.ClientCredentials.UserName.UserName = "a22dmin";
proxy.ClientCredentials.UserName.Password = "w22cf.admin";
string result = proxy.GetData(1);
Console.WriteLine(result);
}
Console.ReadKey();
如果密码账号不匹配,会报错: