使用Windows凭据从ADFS获取SAML令牌

时间:2012-10-16 10:40:53

标签: c# windows-8 sap saml

我正在用c#创建一个Windows 8客户端应用程序。这个应用程序将使用SAP的odata服务。对于身份验证,我需要ADFS发出的SAML令牌。是否有任何方法可以使用Windows凭据从ADFS获取SAML令牌?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码获取SAML令牌。

var factory = new WSTrustChannelFactory(new Microsoft.IdentityModel.Protocols.WSTrust.Bindings.UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential), adfsEndpoint);

factory.Credentials.UserName.UserName = "username";
factory.Credentials.UserName.Password = "********";
factory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
factory.TrustVersion = TrustVersion.WSTrust13;
WSTrustChannel channel = null;
try
{
    var rst = new RequestSecurityToken
    {
        RequestType = WSTrust13Constants.RequestTypes.Issue,
        AppliesTo = new EndpointAddress("https://yourserviceendpoint.com/"),
        KeyType = KeyTypes.Bearer,
    };
    channel = (WSTrustChannel)factory.CreateChannel();
    return channel.Issue(rst);
}
catch (Exception e)
{
    return null;
}