3

我的网站实现了基于 AD FS 的身份验证。现在我需要通过客户端以编程方式访问我的网站。我的客户端应该使用当前登录的用户上下文从 ADFS 服务器请求安全令牌。我已经成功地adfs/services/trust/13/usernamemixed使用客户端的用户名和密码从端点请求安全令牌并将其发布到我的网站。

对我不起作用的是adfs/services/trust/13/windowsmixed使用DefaultNetworkCredentials. 我得到错误The HTTP request was forbidden with client authentication scheme 'Anonymous'.。我正在使用Microsoft.IdentityModelSDK(而不是System.IdentityModel在 .NET 4.5 中)。

这是我的代码片段。

  factory = new MSWSTrustChannelFactory(
  new Microsoft.IdentityModel.Protocols.WSTrust.Bindings.WindowsWSTrustBinding(SecurityMode.TransportWithMessageCredential),
                    stsUrl);

  factory.TrustVersion = TrustVersion.WSTrust13;

  factory.Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

  var rst = new RequestSecurityToken
  {
      RequestType = RequestTypes.Issue,
      AppliesTo = new EndpointAddress(realm),
      KeyType = KeyTypes.Bearer,
      RequestDisplayToken = true
  };

  MSIWSTrustChannelContract channel = factory.CreateChannel();
  RequestSecurityTokenResponse rstr;
  SecurityToken token = channel.Issue(rst, out rstr);

我对 ADFS 服务器没有任何控制权,也无法从那里调试出了什么问题。我能做的只是来自客户端。知道我上面的代码出了什么问题吗?非常感谢任何帮助或指示。

4

1 回答 1

1

我认为您需要将消息安全的建立安全上下文设置为 FALSE
binding.Security.Message.EstablishSecurityContext = false;

以下代码对我有用。

            WS2007HttpBinding binding = new WS2007HttpBinding(SecurityMode.TransportWithMessageCredential);
            binding.Security.Message.EstablishSecurityContext = false;               
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            if (isWindowsUser)
            {
                binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
                ep = new EndpointAddress("https://abc.com/adfs/services/trust/13/windowsmixed");                    
            }
            else
            {
                binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
                ep = new EndpointAddress("https://abc.com/adfs/services/trust/13/usernamemixed");                    
            }
            factory = new WSTrustChannelFactory(binding, ep);
            factory.TrustVersion = TrustVersion.WSTrust13;

                factory.Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;                     


            var rst = new RequestSecurityToken
            {
                RequestType = RequestTypes.Issue,
                AppliesTo = new EndpointReference("urn:adfsmonitor"),
                KeyType = KeyTypes.Bearer,
            };
            IWSTrustChannelContract channel = factory.CreateChannel();
            GenericXmlSecurityToken genericToken = channel.Issue(rst)
             as GenericXmlSecurityToken;
            return genericToken.TokenXml.InnerXml.ToString();
于 2014-09-18T06:28:05.790 回答