dhc*_*cgn 2 c# wcf client-certificates asp.net-core
我需要将客户端证书(例如,不是来自Windows证书存储区)设置到我的wcf通道,但是我总是会收到异常:
System.InvalidOperationException:“对象是只读的。”
这很奇怪,因为这些属性都有一个setter,但是如果我分配了X509Certificate2,则会崩溃。
System.InvalidOperationException
HResult=0x80131509
Nachricht = Object is read-only.
Quelle = System.Private.ServiceModel
Stapelüberwachung:
at System.ServiceModel.Security.X509CertificateRecipientClientCredential.ThrowIfImmutable()
at System.ServiceModel.Security.X509CertificateRecipientClientCredential.set_DefaultCertificate(X509Certificate2 value)
Run Code Online (Sandbox Code Playgroud)
var binding = new BasicHttpsBinding();
var endpoint = new EndpointAddress(new Uri("https://myservice.com"));
var channelFactory = new ChannelFactory<MyService>(binding, endpoint);
var serviceClient = channelFactory.CreateChannel();
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
var token = GetToken(); // Just an method that reads a pfx from disk
channelFactory.Credentials.
ServiceCertificate.DefaultCertificate = token.Certificate; // throws exception
channelFactory.Credentials.
ClientCertificate.Certificate = token.Certificate; // throws exception too
Run Code Online (Sandbox Code Playgroud)
该方法SetCertificate引发相同的System.InvalidOperationException: "Object is read-only."异常。
using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
store.Open(OpenFlags.ReadWrite);
var x509Certificate2Collection = store.Certificates.Find(X509FindType.FindByThumbprint, token.Certificate.Thumbprint, false);
if(x509Certificate2Collection.Count == 0)
store.Add(token.Certificate);
}
channelFactory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My,X509FindType.FindByThumbprint, token.Certificate.Thumbprint);
Run Code Online (Sandbox Code Playgroud)
X509CertificateRecipientClientCredential.cs的实现很有趣。
public X509Certificate2 DefaultCertificate
{
get
{
return _defaultCertificate;
}
set
{
ThrowIfImmutable();
_defaultCertificate = value;
}
}
internal void MakeReadOnly()
{
_isReadOnly = true;
this.Authentication.MakeReadOnly();
if (_sslCertificateAuthentication != null)
{
_sslCertificateAuthentication.MakeReadOnly();
}
}
private void ThrowIfImmutable()
{
if (_isReadOnly)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ObjectIsReadOnly)));
}
}
Run Code Online (Sandbox Code Playgroud)
某种呼唤internal void MakeReadOnly()使我的生活更加艰难。
ClientCredentials.cs在github上阅读时,我找到了方法 MakeReadOnly()。
的调用channelFactory.CreateChannel()使ClientCertificate实例成为只读实例,因此,在更改语句的顺序之后,它便可以工作!
var binding = new BasicHttpsBinding();
var endpoint = new EndpointAddress(new Uri("https://myservice.com"));
var channelFactory = new ChannelFactory<MyService>(binding, endpoint);
// Must set before CreateChannel()
channelFactory.Credentials.
ClientCertificate.Certificate = token.Certificate;
var serviceClient = channelFactory.CreateChannel();
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2206 次 |
| 最近记录: |