使用wsdl与基本身份验证.Net

o4tp2gmn  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(156)

我有一个wsdl与基本的身份验证保护,当我试图与 Postman 或通过浏览器其工作。
为了在我的代码中使用它,我已经通过visual code 2022连接了服务,并创建了代理类。
然后我用下面的代码来使用它的方法

BasicHttpBinding binding = new BasicHttpBinding
        {
            SendTimeout = TimeSpan.FromSeconds(500),
            MaxBufferSize = int.MaxValue,
            MaxReceivedMessageSize = int.MaxValue,
            AllowCookies = true,
            ReaderQuotas = XmlDictionaryReaderQuotas.Max
        };

        binding.Security.Mode = BasicHttpSecurityMode.Transport;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
        EndpointAddress address = new EndpointAddress("https://test/test/.wsdl");
        ChannelFactory<TEST_Channel> factory = new ChannelFactory<TEST_Channel>(binding, address);

        factory.Credentials.UserName.UserName = "abcd";
        factory.Credentials.UserName.Password = "1234";

        var f = factory.CreateChannel();
        f.someMethod(param);

字符串
但是在调用someMethod()时出现以下错误

System.ServiceModel.Security.MessageSecurityException : The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Digest realm="SOAP-Server", nonce="aasdasdasd1231313", algorithm="MD5", qop="auth"'.


如果你能帮忙的话

w8f9ii69

w8f9ii691#

以下是我的解决方案[链接][1]

var client = new TEST_ChannelClient();
        using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
        {
            var httpRequestProperty = new HttpRequestMessageProperty();
            httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " +
                         Convert.ToBase64String(Encoding.ASCII.GetBytes("<userName>" + ":" +
                         "Password"));
            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

            var b = await client.someMethod();
        }

  [1]: https://stackoverflow.com/questions/39354562/consuming-web-service-with-c-sharp-and-basic-authentication/39357477#39357477

字符串

相关问题