SecurityNegotiationException при попытке выдать запрос токена безопасности - PullRequest
0 голосов
/ 21 января 2011

Я пытаюсь отправить запрос на выдачу на мой STS и получаю следующее исключение:

System.ServiceModel.Security.SecurityNegotiationException' occurred in mscorlib.dll but was not handled in user code

Additional information: Could not establish secure channel for SSL/TLS with authority 'service-location-url-here'.

Следующий код работает на веб-сайте asp.net, который выдает себя за мою учетную запись. Служба STS требует взаимной аутентификации, поэтому мне нужно отправить сертификат с моим запросом токена безопасности. Журналы сервера жалуются на то, что к сообщению не прикреплен сертификат, что прерывает операцию.

Где я терплю неудачу здесь?

public class StsRequestWrapper
{
  public System.IdentityModel.Tokens.SecurityToken RequestSecurityToken()
  {
      IgnoreCertificateValidation();

      StsRequestUtil util = new StsRequestUtil();
      var certificate = util.GetLocalCertificate();
      var securityTokenElement = util.BuildSampleSecurityTokenElement();
      var trustChannelFactory = GetTrustChannelFactory(certificate);

      try
      {
          RequestSecurityToken rst = CreateSecurityTokenObject(securityTokenElement);
          WSTrustChannel channel = (WSTrustChannel)trustChannelFactory.CreateChannel();
          RequestSecurityTokenResponse rstr = null;
          return channel.Issue(rst, out rstr);
      }
      finally
      {
          trustChannelFactory.Close();
      }
  }

  private RequestSecurityToken CreateSecurityTokenObject(SecurityTokenElement securityToken)
  {
      RequestSecurityToken rst = new RequestSecurityToken()
      {
          AppliesTo = new EndpointAddress(new Uri(STS_CONSTANTS.APPLIES_TO_URL)),
          ActAs = securityToken,
          RequestType = RequestTypes.Issue,
          Lifetime = new Lifetime(DateTime.UtcNow, DateTime.UtcNow.AddMinutes(5)),
      };

      var requestClaim = new RequestClaim(STS_CONSTANTS.REQUEST_CLAIM_TYPE, false, STS_CONSTANTS.REQUEST_CLAIM_VALUE);
      rst.Claims.Dialect = STS_CONSTANTS.CLAIMS_DIALECT;
      rst.Claims.Add(requestClaim);
      return rst;
  }

  private WSTrustChannelFactory GetTrustChannelFactory(X509Certificate2 localhostCertificate)
  {

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

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

      trustChannelFactory.Credentials.ClientCertificate.Certificate = localhostCertificate;
      trustChannelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust;
      trustChannelFactory.Credentials.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;

      return trustChannelFactory;
  }

  private static void IgnoreCertificateValidation()
  {
      System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
  }

}

1 Ответ

0 голосов
/ 21 января 2011

Оказывается, мне нужно использовать CertificateWSTrustBinding вместо WS2007HttpBinding.

        var binding = new CertificateWSTrustBinding();
        binding.SecurityMode = SecurityMode.Transport;
...