【问题标题】:How to pass a certificate to WSTrust to get Saml Token如何将证书传递给 WSTrust 以获取 Saml Token
【发布时间】:2014-11-05 06:11:42
【问题描述】:

这是一个使用 WSTrustChannelFactory 获取令牌的示例。 From here.

var stsBinding = new WS2007HttpBinding();
stsBinding.Security.Mode = SecurityMode.TransportWithMessageCredential;
stsBinding.Security.Message.EstablishSecurityContext = false;
stsBinding.Security.Message.NegotiateServiceCredential = false;
stsBinding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;


WSTrustChannelFactory trustChannelFactory = new WSTrustChannelFactory(
    stsBinding
    , new EndpointAddress(tokenurl)
    );
trustChannelFactory.TrustVersion = System.ServiceModel.Security.TrustVersion.WSTrust13;

X509Store myStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
myStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection coll = myStore.Certificates.Find(X509FindType.FindBySerialNumber, "MycertSerialNumber", true);
X509Certificate2 cert = coll[0];
trustChannelFactory.Credentials.ClientCertificate.Certificate = cert;

WSTrustChannel channel = (WSTrustChannel)trustChannelFactory.CreateChannel();

RequestSecurityToken rst = new RequestSecurityToken(RequestTypes.Issue, keyType);
rst.AppliesTo = new EndpointAddress(realm);
RequestSecurityTokenResponse rstr = null;
rst.TokenType = SecurityTokenTypes.Saml;

SecurityToken token = channel.Issue(rst, out rstr);

现在我没有用户名/密码,但提供商给了我证书 .pfx 文件。 如何将它传递给 WSTrushChannelFactory?我尝试过使用 CertificateBinding 但没有成功。

以上更新代码:11/05/2014:

收到此错误:ID3242:无法验证或授权安全令牌。

【问题讨论】:

    标签: wcf wif saml ws-trust


    【解决方案1】:

    使用ClientCertificate 属性:

    var stsBinding = new WS2007HttpBinding();
    stsBinding.Security.Mode = SecurityMode.TransportWithMessageCredential;
    stsBinding.Security.Message.EstablishSecurityContext = false;
    stsBinding.Security.Message.NegotiateServiceCredential = false;
    
    // select the authentication mode of Client Certificate
    stsBinding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
    
    var wifChannelFactory = new WSTrustChannelFactory(stsBinding, stsEndpoint);
    wifChannelFactory.TrustVersion = TrustVersion.WSTrust13;
    
    // Supply the credentials
    wifChannelFactory.Credentials.ClientCertificate.Certificate = config.Certificate;
    

    您可以通过certmgr.msc 管理单元import to your certificate 存储PFX。确保您的应用程序运行的帐户为has access to the private key。您可以使用x509certificate2 类来reference it in the store

    【讨论】:

    • Mitch,您的建议让我比以前更进了一步,但现在出现此错误:ID3242:无法对安全令牌进行身份验证或授权。
    • @gbs,我假设您的意思是当您尝试使用收到的令牌时遇到该错误。 ID3242 通常是由指定的错误受众 uri 引起的。确保您的AppliesTo 与 STS 期望的内容以及 RP 配置接受的内容相匹配。另一个可能是 STS 上配置的签名或加密证书与 RP 不匹配。
    • 不使用但请求令牌。 STS 正在向我发送该错误。我已将其发送给提供商,他们也在调查。
    • @gbs,你找到解决方案了吗?这显然不是我以前见过的问题,并且会对您如何解决它感兴趣。
    • 还没有。我正在等待提供商,因为他们也在调查他们的 ADFS。我会在获得更多信息和/或让它正常工作后立即更新。
    【解决方案2】:

    给你。

    private static SecurityToken RequestSecurityToken()    
    {    
        // set up the ws-trust channel factory    
        var factory = new WSTrustChannelFactory(    
            new UserNameWSTrustBinding(
              SecurityMode.TransportWithMessageCredential),    
              _idpAddress);    
        factory.TrustVersion = TrustVersion.WSTrust13;            
    
        var authCertificate = X509.LocalMachine.My.Thumbprint.Find(Properties.Settings.Default.RassCertificateThumbprint).FirstOrDefault();
        if (authCertificate == null)
            throw new InternalException(String.Format("No atuhentication certificate found in store with thumbprint {0}.", Properties.Settings.Default.ClientCertificateThumbprint));
    
        // overenie je na zaklade certifikatu RASS
        factory.Credentials.ClientCertificate.Certificate = authCertificate;
    
        // create token request  
        var rst = new RequestSecurityToken    
        {    
            RequestType = RequestTypes.Issue,
            KeyType = KeyTypes.Symmetric,    
            AppliesTo = new EndpointReference(_serviceAddress.AbsoluteUri)    
        };
    
        // request token and return
        return factory.CreateChannel().Issue(rst);    
    }
    

    顺便说一句:@Mitch 关于访问私钥是正确的。我只是采用了你的方法,替换了几行代码。

    【讨论】:

    • pepo,我按照 mitch 的建议更新了上面的代码,但现在出现错误。
    • 你在哪里得到这个错误。是在SecurityToken token = channel.Issue(rst, out rstr); 上还是在您尝试使用收到的令牌时。
    • 您是否有权访问证书的私钥?
    • 我想我应该有。我从提供商那里获得了带有密码的 .pfx 文件。无论如何,我如何得到那个钥匙?哦,我实际上按照上面 Mitch 的链接向所有人授予了商店中的证书的权限。当我在商店中导入 .pfx 时,我确实选择了“允许导出私钥”
    • X509certificate2 类有一个属性 PrivateKey。如果您可以访问它(即在 Visual Studio 的调试模式下),那么您可以访问它。还要检查证书是否受信任。在 mmc 控制台中查看证书的详细信息,看看您是否拥有整个证书链。
    猜你喜欢
    • 2017-09-24
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    相关资源
    最近更新 更多