当然,您只需要做一点步法就可以开始使用自定义安全绑定扩展元素来描述令牌,以及哪一个应该用于签名/签名。为了解释的目的,我会假设你总是希望通过证书和用户名/密码。
在自定义绑定元素中,您需要创建一个TransportSecurityBindingElement
并向其添加令牌参数。有三个集合可将标记参数添加到:SignedEncrypted
,Signed
和Endorsing
。对于我们在此讨论的场景,我建议将UserNameSecurityTokenParameters
添加到SignedEncrypted
集合,将X509SSecurityTokenParameters
添加到Endorsing
集合。这意味着消息的有效性/完整性由证书令牌提供,而不是由用户名/密码提供,用户名/密码令牌将由证书令牌加密的签名和。这看起来像以下:
public class MySecurityBindingElement : BindingElementExtensionElement
{
public override void ApplyConfiguration(BindingElement bindingElement)
{
base.ApplyConfiguration(bindingElement);
TransportSecurityBindingElement transportSecurityBindingElement = (TransportSecurityBindingElement)bindingElement;
transportSecurityBindingElement.EndpointSupportingTokenParameters.SignedEncrypted.Add(new UserNameSecurityTokenParameters());
transportSecurityBindingElement.EndpointSupportingTokenParameters.Endorsing.Add(new X509SecurityTokenParameters
{
InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient,
ReferenceStyle = SecurityTokenReferenceStyle.Internal,
RequireDerivedKeys = false,
X509ReferenceStyle = X509KeyIdentifierClauseType.Any
});
}
protected override BindingElement CreateBindingElement()
{
TransportSecurityBindingElement result = new TransportSecurityBindingElement
{
IncludeTimestamp = true,
LocalClientSettings.DetectReplays = false,
LocalServiceSettings.DetectReplays = false
};
this.ApplyConfiguration(result);
return result;
}
}
然后,从客户的角度来看,你只要确保设置证书和用户名密码获取您使用的是与服务器通信每个通道设置。您可以在运行时通过设置ChannelFactory
或标准WCF,ClientBase
proxy类的the Credentials
property上的属性来完成此操作。你可以做的另一件事是通过端点行为设置客户端证书,像这样:
使用这种方式时,你将只需要明确在运行时设置的用户名/密码。
最后,在STS处,您可以读取用户通过OperationContext::SupportingTokens
属性指定的令牌。您会在集合中找到UserNameSecurityToken
和X509SecurityToken
的实例,然后您可以使用这些实例来验证呼叫者身份。
感谢您的快速回答!我没有想到使用自定义安全绑定。那么明天我会试试看。我希望尽管有一种“内置”方式。 – R4cOON 2011-05-24 15:23:54
开箱即用,但至少有旋钮和开关在那里。;) – 2011-05-24 15:30:07
所以这适用于自定义STS或可以通过ADFS v2.0完成? – nzpcmad 2011-05-24 19:15:16