我正在开发一项服务以在 WCF 中使用基于 SSL/TLS 凭据的安全性。将使用 IIS 和 HTTPS 访问端点。基本的 HTTP 绑定(bind)工作正常,但只要我尝试添加凭据,我就会不断收到配置错误。我在这上面花了几天时间,也阅读了很多文章,但都没有成功。
我试过了
//代码隐藏
public class specificService : myServiceType
{
public static void Configure(ServiceConfiguration configuration)
{
// define https binding (I think it should be BasicHttpsBinding as opposed to BasicHttpBinding which also supports TLS but I am unsure)
var httpsBinding = new BasicHttpsBinding
{
MaxReceivedMessageSize = int.MaxValue,
Security = new BasicHttpsSecurity
{
Mode = BasicHttpsSecurityMode.Transport,
Transport = new HttpTransportSecurity
{
ClientCredentialType = HttpClientCredentialType.Certificate
}
}
};
configuration.AddServiceEndpoint(typeof(myServiceType), httpsBinding, string.Empty);
configuration.Description.Endpoints.Find(typeof(myServiceType))?.EndpointBehaviors.Add(new myNamespace.MyCustomEndpointBehavior());
configuration.Description.Behaviors.Add(new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true });
configuration.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
// Do I need to explicitly add my certificate?
X509Certificate2 myCert = ... get cert from local machine store
configuration.Credentials.ServiceCertificate.Certificate = myCert;
}
/* myServiceType interface */
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://somenamespace", ConfigurationName="someothernamespace.Services.myServiceType")]
public interface myServiceType
{
// CODEGEN: Generating message contract since the operation specificAction is neither RPC nor document wrapped.
[System.ServiceModel.OperationContractAttribute(Action="http://somenamespace/specificAction")]
someothernamespace.Services.specificActionResponse specificAction(someothernamespace.Services.specificAction request);
}
我不断收到“配置错误”,但没有详细说明我究竟做错了什么。我尝试了不同的实现 (wsHttpBindings) 但没有成功。
最佳答案
虽然WCF4.5宣布我们可以在服务实现类中使用静态Configure方法来设置服务,但我建议您在配置文件中配置服务。简洁明了。
// Do I need to explicitly add my certificate?
您不需要设置证书,因为传输安全模式需要 IIS 站点绑定(bind)模块中的 https 基地址。
此外,如果您想使用证书对客户端进行身份验证,请引用以下链接。
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/transport-security-with-certificate-authentication
我们应该首先建立服务器和客户端之间的信任关系,然后客户端提供服务器信任的证书。由于Makecert命令行已经过时,我建议您使用PowerShell创建一个自签名证书,并确保客户端和服务器运行时环境在dotnetframwork4.6.2以上。
https://learn.microsoft.com/en-us/powershell/module/pkiclient/new-selfsignedcertificate?view=win10-ps
https://github.com/dotnet/docs/issues/12000
如果有什么我可以帮忙的,请随时告诉我。
关于c# - WCF 使用凭据和 IIS 以编程方式启用 TLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57258121/