gpt4 book ai didi

wcf - 通过 ssl 验证的自定义 WCF 客户端证书

转载 作者:太空宇宙 更新时间:2023-11-03 15:04:47 25 4
gpt4 key购买 nike

你好,

我正在尝试使用 WCF 进行一些身份验证:

  • 使用用户名/密码验证用户
  • 使用客户端证书对客户端进行身份验证
  • 自定义接受哪些根证书

经过反复试验,我设法让第 1 点和第 2 点正常工作,但我卡在了第 3 点。这是我的服务配置

<system.serviceModel>
<behaviors>
<endpointBehaviors />
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="WcfService1.CustomValidator, WcfService1" />
</serviceCredentials>
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="certificate">
<security authenticationMode="UserNameOverTransport" />
<textMessageEncoding messageVersion="Soap12WSAddressing10" />
<httpsTransport requireClientCertificate="true" />
</binding>
</customBinding>
</bindings>
<services>
<service behaviorConfiguration="MyBehavior" name="WcfService1.Service1">
<endpoint address="" binding="customBinding" bindingConfiguration="certificate"
contract="WcfService1.IService1" />
</service>
</services>
</system.serviceModel>

这是我的客户端配置

    <client>
<endpoint name="service1" address="https://localhost:443/WcfService1/Service1.svc" binding="customBinding"
bindingConfiguration="certificate" behaviorConfiguration="certificate" contract="WcfService1.IService1" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="certificate">
<clientCredentials>
<clientCertificate storeName="My" storeLocation="LocalMachine" x509FindType="FindBySubjectName"
findValue="SignedByCA" />
</clientCredentials>
</behavior>
</endpointBehaviors>
<serviceBehaviors />
</behaviors>
<bindings>
<customBinding>
<binding name="certificate">
<security authenticationMode="UserNameOverTransport" />
<textMessageEncoding messageVersion="Soap12WSAddressing10" />
<httpsTransport requireClientCertificate="true" />
</binding>
</customBinding>
</bindings>

使用客户端并附加用户名凭证效果很好

var channelFactory = new ChannelFactory<IService1>("service1");
var user = channelFactory.Credentials.UserName;
user.UserName = username;
user.Password = password;

使用 OperationContext.Current.ServiceSecurityContext.AuthorizationContext.ClaimSets 让我可以访问用户名以及证书的名称和指纹。遗憾的是,我无法找到证书的 IssuerName。我还能如何禁止没有特定根证书颁发的证书的客户端?

非常欢迎任何指向正确方向或任何替代方案的提示;)

谢谢

最佳答案

实际上,这很容易,但是很麻烦。授权上下文中有一个身份列表。

OperationContext.Current.ServiceSecurityContext
.AuthorizationContext.Properties["Identities"]

其中一个是 X509Identity 类型。那个是 System.IdentityModel 内部的,您不能直接获取它。

identity.GetType().Name == "X509Identity"

这并不重要,因为无论如何,包含证书的字段是私有(private)的:)

var field = identity.GetType().GetField(
"certificate",
BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);
var certificate = (X509Certificate2) field.GetValue(identity);
string issuer = certificate.Issuer;

关于wcf - 通过 ssl 验证的自定义 WCF 客户端证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11966576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com