简体   繁体   中英

Problem with authentication on java ws

I have application in C# that consume Java WS. Everything worked fine until WS was configured to use authentication. Now I should user login i password to execute methods from WS but I'm not sure how to do it. I've try

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

But I get SecurityMessageException-{"The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM'."} InnerException - (WebException) - {"The remote server returned an error: (401) Unauthorized."}.

What's wrong?

Thanks

Try this:

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", "", "", "");

UPDATE:

Sorry in my first post I thought you were using wsdl.exe to generate the client proxy. For a WCF client you need to configure the endpoint:

 
 
 
  
  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:

I've used the following configuration to invoke web services protected with NTLM authentication. In app.config of the client put the following:

<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>

and then you could set the corresponding credentials before invoking the method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM