[英]Problem with authentication on java ws
我有使用Java WS的C#应用程序。 在将WS配置为使用身份验证之前,一切工作正常。 现在,我应该使用用户登录名i密码来执行WS中的方法,但是我不确定该怎么做。 我尝试过
var client = new MyBeanClient();
client.ClientCredentials.UserName.UserName = "admin";
client.ClientCredentials.UserName.Password = "";
client.addConsumer("whatever", "", "", "");
但是我收到了SecurityMessageException-{“ HTTP请求是使用客户端身份验证方案'Anonymous'未经授权的。从服务器收到的身份验证标头是'Negotiate,NTLM'。”} InnerException-(WebException)-{“远程服务器返回错误:(401)未经授权。”}。
怎么了?
谢谢
尝试这个:
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", "", "", "");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.