使用Windows传递身份验证为依赖方信任检索SAML令牌

时间:2019-07-09 18:38:24

标签: c# token saml adfs

我正在尝试为应用程序(代表用户)代表应用程序的当前上下文确定一种方法来检索SAML令牌以访问依赖方信任。

通常的建议是使用WSTrustChannelFactory检索SAML令牌,但要提供用户名和密码。我不想这样做,因此对用户是无缝的。我觉得ADFS应该为此请求支持SSO身份验证(使用直通Windows身份验证)。这可能吗?

以下是需要凭据的典型推荐代码:

public SecurityToken GetSamlToken()
{
        using (var factory = new WSTrustChannelFactory(
        new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential),
        new EndpointAddress(new Uri("https://serv/adfs/services/trust/13/usernamemixed"))))
        {
        factory.Credentials.UserName.UserName = "username";
        factory.Credentials.UserName.Password = "password";
        factory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
        factory.TrustVersion = TrustVersion.WSTrust13;                
        WSTrustChannel channel = null;                
        try
        {
            string KeyType;
            var rst = new RequestSecurityToken
                          {
                              RequestType = WSTrust13Constants.RequestTypes.Issue,
                              AppliesTo = new EndpointAddress("net.tcp://localhost:xxxx/Service1/mex"),                         
                              KeyType = Microsoft.IdentityModel.Protocols.WSTrust.WSTrust13Constants.KeyTypes.Bearer,                                        
                          };

            channel = (WSTrustChannel)factory.CreateChannel();

            return channel.Issue(rst);
        }
        finally
        {
            if (channel != null)
            {
                channel.Abort();
            }

            factory.Abort();
        }
    }
}

谢谢您的帮助!

0 个答案:

没有答案