Wcf 从服务器接收的身份验证标头。。BASIC-WFC-NET内核

Wcf 从服务器接收的身份验证标头。。BASIC-WFC-NET内核,wcf,.net-core,service,Wcf,.net Core,Service,我正在使用.NETCore的WCF(连接服务),我想调用wfc服务并发送xml。。。但当执行服务时, 这将引发下一个异常 从服务器接收的身份验证标头为“Basic realm=“AXIS” 当我在SOAP UI中运行服务时,我分配了基本授权选项,它工作得非常好。。。但在这一部分中,我不知道如何设置授权类型 我也没有任何配置xml文件,只有一个名为ConnectedService.json的json 代码: 首先,确保帐户名和密码正确,您还可以使用域/用户 client.ClientCredent

我正在使用.NETCore的WCF(连接服务),我想调用wfc服务并发送xml。。。但当执行服务时, 这将引发下一个异常

从服务器接收的身份验证标头为“Basic realm=“AXIS”

当我在SOAP UI中运行服务时,我分配了基本授权选项,它工作得非常好。。。但在这一部分中,我不知道如何设置授权类型

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

代码:


首先,确保帐户名和密码正确,您还可以使用域/用户

client.ClientCredentials.UserName.UserName = @"domain\username";
client.ClientCredentials.UserName.Password  = "password";
其次,请检查自动生成的服务端点。它应该位于
参考.cs
中。构造函数中的参数是一个
枚举
,它指示应该使用哪个端点

ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(ServiceReference1.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1);
client.ClientCredentials.UserName.UserName = "administrator";
client.ClientCredentials.UserName.Password = "abcd1234!";
最后,我们还可以在soap请求中手动添加基本Http头

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();

                }
如果问题仍然存在,请随时通知我

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();

                }