ServiceCredentialsSecurityTokenManager.CreateSecurityTokenProvider 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
基于 SecurityTokenRequirement 创建安全令牌提供程序。
public:
override System::IdentityModel::Selectors::SecurityTokenProvider ^ CreateSecurityTokenProvider(System::IdentityModel::Selectors::SecurityTokenRequirement ^ requirement);
public override System.IdentityModel.Selectors.SecurityTokenProvider CreateSecurityTokenProvider (System.IdentityModel.Selectors.SecurityTokenRequirement requirement);
override this.CreateSecurityTokenProvider : System.IdentityModel.Selectors.SecurityTokenRequirement -> System.IdentityModel.Selectors.SecurityTokenProvider
Public Overrides Function CreateSecurityTokenProvider (requirement As SecurityTokenRequirement) As SecurityTokenProvider
参数
- requirement
- SecurityTokenRequirement
安全令牌需求。
返回
安全令牌提供程序。
例外
requirement
为 null
。
无法为传入的 requirement
创建安全令牌提供程序。
示例
下面的代码演示如何重写此方法。
internal class MyServiceCredentialsSecurityTokenManager :
ServiceCredentialsSecurityTokenManager
{
MyServiceCredentials credentials;
public MyServiceCredentialsSecurityTokenManager(
MyServiceCredentials credentials)
: base(credentials)
{
this.credentials = credentials;
}
public override SecurityTokenProvider CreateSecurityTokenProvider(
SecurityTokenRequirement requirement)
{
SecurityTokenProvider result = null;
if (requirement.TokenType == SecurityTokenTypes.X509Certificate)
{
MessageDirection direction = requirement.
GetProperty<MessageDirection>(
ServiceModelSecurityTokenRequirement.
MessageDirectionProperty);
if (direction == MessageDirection.Input)
{
if (requirement.KeyUsage == SecurityKeyUsage.Exchange)
{
result = new X509SecurityTokenProvider(
credentials.ServiceEncryptingCertificate);
}
else
{
result = new X509SecurityTokenProvider(
credentials.ClientSigningCertificate);
}
}
else
{
if (requirement.KeyUsage == SecurityKeyUsage.Signature)
{
result = new X509SecurityTokenProvider(
credentials.ServiceSigningCertificate);
}
else
{
result = new X509SecurityTokenProvider(
credentials.ClientEncryptingCertificate);
}
}
}
else
{
result = base.CreateSecurityTokenProvider(requirement);
}
return result;
}
}
Friend Class MyServiceCredentialsSecurityTokenManager
Inherits ServiceCredentialsSecurityTokenManager
Private credentials As MyServiceCredentials
Public Sub New(ByVal credentials As MyServiceCredentials)
MyBase.New(credentials)
Me.credentials = credentials
End Sub
Public Overrides Function CreateSecurityTokenProvider(ByVal requirement As SecurityTokenRequirement) As SecurityTokenProvider
Dim result As SecurityTokenProvider = Nothing
If requirement.TokenType = SecurityTokenTypes.X509Certificate Then
Dim direction As MessageDirection = requirement. GetProperty(Of MessageDirection)(ServiceModelSecurityTokenRequirement. MessageDirectionProperty)
If direction = MessageDirection.Input Then
If requirement.KeyUsage = SecurityKeyUsage.Exchange Then
result = New X509SecurityTokenProvider(credentials.ServiceEncryptingCertificate)
Else
result = New X509SecurityTokenProvider(credentials.ClientSigningCertificate)
End If
Else
If requirement.KeyUsage = SecurityKeyUsage.Signature Then
result = New X509SecurityTokenProvider(credentials.ServiceSigningCertificate)
Else
result = New X509SecurityTokenProvider(credentials.ClientEncryptingCertificate)
End If
End If
Else
result = MyBase.CreateSecurityTokenProvider(requirement)
End If
Return result
End Function
End Class
注解
SecurityTokenProvider 类负责获取给定的 ServiceModelSecurityTokenRequirement 的令牌。 令牌提供程序还可以支持续订和取消令牌。 如果进行了相应的配置,令牌提供程序可以缓存令牌。