java ws上的身份验证问题

时间:2009-12-20 09:25:35

标签: c# java web-services authentication

我在C#中使用Java WS的应用程序。一切正常,直到WS配置为使用身份验证。现在我应该用户登录我的密码从WS执行方法,但我不知道该怎么做。 我试过

var client = new MyBeanClient();
                    client.ClientCredentials.UserName.UserName = "admin";
                    client.ClientCredentials.UserName.Password = "";
                    client.addConsumer("whatever", "", "", "");

但我明白了 SecurityMessageException - {“HTTP请求未经授权与客户端 认证方案'匿名'。收到的身份验证标头 服务器是'Negotiate,NTLM'。“} InnerException - (WebException) - {“远程服务器返回错误: (401)未经授权。“}。

怎么了?

由于

1 个答案:

答案 0 :(得分:0)

试试这个:

var credentialCache = new CredentialCache();
var credentials = new NetworkCredential("username", "password", "domain");
credentialCache.Add(new Uri(client.Url), "NTLM", credentials);
client.Credentials = credentialCache;
client.addConsumer("whatever", "", "", "");

更新:

在我的第一篇文章中抱歉,我以为您使用wsdl.exe来生成客户端代理。对于WCF客户端,您需要配置端点:

var basicHttpBinding = new BasicHttpBinding();
basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
var endpoint = new EndpointAddress("http://example.com/myWindowsAuthN");
var client = new MyBeanClient(basicHttpBinding, endpoint);
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

client.ChannelFactory.Credentials.Windows.ClientCredential.Domain = "domain";
client.ChannelFactory.Credentials.Windows.ClientCredential.UserName = "username";
client.ChannelFactory.Credentials.Windows.ClientCredential.Password = "password";

<击>


UPDATE2:

我使用以下配置来调用受NTLM身份验证保护的Web服务。在客户端的app.config中输入以下内容:

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="NtlmBinding">
        <security mode="TransportCredentialOnly">
          <transport clientCredentialType="Ntlm" />
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>
  <client>
    <endpoint
      address="http://example.com/SomeWindowsAuthenticatedService"
      binding="basicHttpBinding"
      bindingConfiguration="NtlmBinding"
      contract="IOperationContractOfTheService"
      name="WSTestSoap" />
  </client>
</system.serviceModel>

然后您可以在调用方法之前设置相应的凭据:

using (var client = new MyBeanClient())
{
    client.ChannelFactory.Credentials.Windows.ClientCredential = 
        new NetworkCredential("username", "password", "DOMAIN");
    client.addConsumer("whatever", "", "", "");
}