WIF WSTrustSerializationException - 声明方言无法设置为自定义值?

发布于 2024-10-12 20:57:52 字数 2609 浏览 5 评论 0原文

我有以下代码尝试使用 WIF 发出“问题”请求。

当我运行它时,出现以下异常。是否可以使用带有自定义声明的问题来请求安全令牌?

Additional information: ID3257: RequestSecurityToken contains at least one Claim with a Claim value specified but the RequestClaimCollection.Dialect is set to 'urn:custom_namespace:sts:1_0'. The RequestClaimCollection.Dialect must be set to 'http://docs.oasis-open.org/wsfed/authorization/200706/authclaims' for the value to be serialized out.

代码:

private const string CLAIMS_DIALECT = "urn:custom_namespace:sts:1_0";
private const string REQUEST_CLAIM_TYPE = "urn:custom_namespace:sts:1_0";
private const string REQUEST_CLAIM_VALUE = "urn:oasis:names:tc:SAML2.0:consent:current-explicit";


public System.IdentityModel.Tokens.SecurityToken RequestSecurityToken(string input)
{
    System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);

    WS2007HttpBinding binding = new WS2007HttpBinding();
    binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
    binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;

    var trustChannelFactory = new WSTrustChannelFactory(binding, new EndpointAddress(new Uri(STS_URL)));
    trustChannelFactory.TrustVersion = TrustVersion.WSTrust13;

    trustChannelFactory.Credentials.ClientCertificate.Certificate = GetCertificateBySubjectName(LOCALHOST_CERTIFICATE_SUBJECT_NAME);
    trustChannelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust;
    trustChannelFactory.Credentials.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;

    try
    {
        RequestSecurityToken rst = new RequestSecurityToken();

        rst.AppliesTo = new EndpointAddress(new Uri(APPLIES_TO_URL), new X509CertificateEndpointIdentity(GetCertificateBySubjectName(LOGON_SERVICE_CERTIFICATE_SUBJECT_NAME)));
        rst.ActAs = BuildSecurityTokenElementFromInput(input);
        rst.RequestType = RequestTypes.Issue;
        rst.Lifetime = new Lifetime(DateTime.UtcNow, DateTime.UtcNow.AddMinutes(5));
        rst.Claims.Dialect = CLAIMS_DIALECT;
        var requestClaim = new RequestClaim(REQUEST_CLAIM_TYPE, false, REQUEST_CLAIM_VALUE);
        rst.Claims.Add(requestClaim);

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

        RequestSecurityTokenResponse rstr = null;

        return channel.Issue(rst, out rstr);
    }
    finally
    {
        trustChannelFactory.Close();
    }
}

I have the following code trying to make an "Issue" request with WIF.

When I run it I get the following exception. Isn't it possible to request a Security Token using Issue with a custom claim?

Additional information: ID3257: RequestSecurityToken contains at least one Claim with a Claim value specified but the RequestClaimCollection.Dialect is set to 'urn:custom_namespace:sts:1_0'. The RequestClaimCollection.Dialect must be set to 'http://docs.oasis-open.org/wsfed/authorization/200706/authclaims' for the value to be serialized out.

The code:

private const string CLAIMS_DIALECT = "urn:custom_namespace:sts:1_0";
private const string REQUEST_CLAIM_TYPE = "urn:custom_namespace:sts:1_0";
private const string REQUEST_CLAIM_VALUE = "urn:oasis:names:tc:SAML2.0:consent:current-explicit";


public System.IdentityModel.Tokens.SecurityToken RequestSecurityToken(string input)
{
    System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);

    WS2007HttpBinding binding = new WS2007HttpBinding();
    binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
    binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;

    var trustChannelFactory = new WSTrustChannelFactory(binding, new EndpointAddress(new Uri(STS_URL)));
    trustChannelFactory.TrustVersion = TrustVersion.WSTrust13;

    trustChannelFactory.Credentials.ClientCertificate.Certificate = GetCertificateBySubjectName(LOCALHOST_CERTIFICATE_SUBJECT_NAME);
    trustChannelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust;
    trustChannelFactory.Credentials.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;

    try
    {
        RequestSecurityToken rst = new RequestSecurityToken();

        rst.AppliesTo = new EndpointAddress(new Uri(APPLIES_TO_URL), new X509CertificateEndpointIdentity(GetCertificateBySubjectName(LOGON_SERVICE_CERTIFICATE_SUBJECT_NAME)));
        rst.ActAs = BuildSecurityTokenElementFromInput(input);
        rst.RequestType = RequestTypes.Issue;
        rst.Lifetime = new Lifetime(DateTime.UtcNow, DateTime.UtcNow.AddMinutes(5));
        rst.Claims.Dialect = CLAIMS_DIALECT;
        var requestClaim = new RequestClaim(REQUEST_CLAIM_TYPE, false, REQUEST_CLAIM_VALUE);
        rst.Claims.Add(requestClaim);

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

        RequestSecurityTokenResponse rstr = null;

        return channel.Issue(rst, out rstr);
    }
    finally
    {
        trustChannelFactory.Close();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃酥萝莉 2024-10-19 20:57:52

我不确定您是否需要更改 rst.Claims.Dialect 才能完成这项工作。如果将其保留为默认值会怎样?

I'm not sure you need to change the rst.Claims.Dialect in order to make this work. What happens if you leave it as default?

苍景流年 2024-10-19 20:57:52

您想要将请求的声明添加到 RequestSecurityToken。这意味着STS应该发行带有指定声明的令牌。
基本上没有必要,但如果你确定的话,你必须设置方言。确实如此。

You want to add requsted claims to RequestSecurityToken. It means the STS shsould issue token with specified claims.
Basically there is not need to to it but if you are shure you must set dialect. That is true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文