从服务器接收到的身份验证标头.. BASIC - WFC - NET CORE

问题描述 投票:0回答:2

我使用 WCF(连接服务)和 .NET CORE,我想调用 wfc 服务并发送 xml...但是当执行服务时, 这抛出下一个异常

“从服务器收到的身份验证标头是‘Basic realm="AXIS"'。”

当我在 SOAP UI 中运行该服务时,我分配了基本授权选项并且它工作得很好......但是在这部分我不知道如何设置授权类型

我也没有任何配置 xml 文件,只有一个名为 ConnectedService.json 的 json

代码:

var endpoint = new TestService.WebTest_Service.EndpointConfiguration();
var service = new TestService.WebTest_Service(endpoint);

string strXml = _getXml();
TestService.XML xml = new TestService.XML();
xml.document = strXml;

service.ClientCredentials.UserName.UserName = "username";
service.ClientCredentials.UserName.Password = "password";

var result = service.invokeAsync(xml); // => THROW ERROR
wcf .net-core service
2个回答
2
投票

首先确保账户名和密码正确,也可以使用域/用户。

client.ClientCredentials.UserName.UserName = @"domain\username";
client.ClientCredentials.UserName.Password  = "password";

其次,请检查自动生成的服务端点。它应该位于

Reference.cs
。构造函数中的参数是一个
enum
,表示应该使用哪个端点。

ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(ServiceReference1.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1);
client.ClientCredentials.UserName.UserName = "administrator";
client.ClientCredentials.UserName.Password = "abcd1234!";

最后,我们也可以在 soap 请求中手动添加 Basic Http header。

client.ClientCredentials.UserName.UserName = "UserName";
client.ClientCredentials.UserName.Password = "Password";

using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
   HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
   httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(client.ClientCredentials.UserName.UserName + ":" + client.ClientCredentials.UserName.Password));
   OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
                   
   // below is a sample call
   int response = client.addNumbers(1, 2);
   Console.WriteLine(response);
   Console.ReadLine();                    
}

如果问题仍然存在,请随时告诉我。


0
投票
System.ServiceModel.BasicHttpBinding result = null!;
result = new System.ServiceModel.BasicHttpBinding();
result.MaxBufferSize = int.MaxValue;
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
result.MaxReceivedMessageSize = int.MaxValue;
result.AllowCookies = true;
result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Basic;
result.Security.Transport.ProxyCredentialType = System.ServiceModel.HttpProxyCredentialType.None;
XXXXSoapClient.EndpointConfiguration endpointConfiguration = new XXXXSoapClient.EndpointConfiguration();

XX.XXXXSoapClient myobj= new XX.XXXXSSoapClient(endpointConfiguration);
myobj.Endpoint.Binding = result;
.`enter code here`
.
.
 /****Further you may call web service method
© www.soinside.com 2019 - 2024. All rights reserved.