.net core 2和更高版本:具有NTLM授权的连接服务WcfServiceClient SOAP如何? [英] .net core 2 and Higher : connected services WcfServiceClient SOAP with NTLM Authorization how to?

查看:69
本文介绍了.net core 2和更高版本:具有NTLM授权的连接服务WcfServiceClient SOAP如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在.net core 2.1上运行一个应用程序.我通过连接的服务添加了wsdl Web服务,该服务成功为我生成了WcfServiceClient.

I'm running an application on .net core 2.1. I added a wsdl web service through connected services that generated me a WcfServiceClient successfully.

使用基本自动化时,它可以精细.

这是我用来调用helloword soap方法的类:

here is the class I use for calling a helloword soap method :

public string HellowWorld(string input)
{
    string wsRes = null;
    try
    {
        var service = new WorkerProcessServiceClient();
        var url = $"http://ServerUrl/Directory/WsName.svc";
        UriBuilder uriBuilder = new UriBuilder(url);

        service.Endpoint.Address = new EndpointAddress(uriBuilder.Uri);
        service.ClientCredentials.UserName.UserName = Username;
        service.ClientCredentials.UserName.Password = Password;

        using (OperationContextScope scope = new OperationContextScope(service.InnerChannel))
        {
            HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
            httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] =
                "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(service.ClientCredentials.UserName.UserName
                + ":"
                + service.ClientCredentials.UserName.Password));
            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
            wsRes = service.HelloWorldAsync(input, RetailContext).GetAwaiter().GetResult();
            service.Close();
        }
    }
    catch (Exception ex)
    {
        wsRes = ex.Message;
    }
    return wsRes;
}

这对于在基本授权上运行的服务器可以很好地工作.我在 SOAP UI 中使用了相同的凭据,并且效果很好.而且我什至不需要指定

This is working fine with servers that are running on Basic Authorization. I am using the same credentials with SOAP UI and it is working very well. and I dont even need to specify the

< ==>现在是问题< =>

<==> Now The Problem <=>

我有第二台服务器,该服务器以 NTLM授权运行.我完成了所有的工作:'(但似乎没有任何作用.

I have a second server that runs with NTLM Authorization. I done it all :'( but nothing seems working.

1-我将 service.clientCredential.Username 更改为 service.clientCredential.Windows ,并添加了 service.clientCredential.Windows.domain

1 - I changed my service.clientCredential.Username to service.clientCredential.Windows and I added service.clientCredential.Windows.domain

2-我还将标头也从基本" +转换... 更改为"Ntlm" +转换...

2 - I changed the Header also from "Basic " + Convert... to "Ntlm " + Convert...

3-我在标题中添加了域,并将其放在第一个和最后一个位置.

3 - I added the domain in the header and I put it first and last position.

当我使用 SOAP UI 时,它工作正常.

when I use SOAP UI, it is working just fine.

我不知道该怎么办,请帮忙.

I dont know what to do else Please Help.

推荐答案

我终于找到了.

这里是我的新代码,用于通过NTLM授权获得服务

So here My new Code to get the service with NTLM Authorization

    private WcfServiceClient MyNtlmConfiguredService()
    {
        BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
        basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
        //this is for enabling Ntlm if you wanna work with basic you just 
        // you just replace HttpClientCredentialType.Ntlm by HttpClientCredentialType.Basic
        basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

        EndpointAddress endpoint = new EndpointAddress("http://ServerUrl/Directory/WsName.svc");

        var client = new WcfServiceClient(basicHttpBinding, endpoint);

        NetworkCredential myCreds = new NetworkCredential("Username", "pas**rd", "Domain");

        client.ClientCredentials.Windows.ClientCredential = myCreds;
        client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

        return client;
    }

然后您正常调用WebService

and then you call your WebService normaly

MyNtlmConfiguredService().HellowWorld(input).getAwaiter().getResult();

现在进行基本授权:

    private CustomerWcfServiceClient MyBasicConfiguredService()
    {
        var service = new CustomerWcfServiceClient();
        CustomerWcfServiceClient client = null;
        string wsRes = null;

        BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
        basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;//mandatory
        basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;//mandatory

        EndpointAddress endpoint = new EndpointAddress("http://ServerUrl/Directory/WsName.svc");

        client = new CustomerWcfServiceClient(basicHttpBinding, endpoint);


        client.ClientCredentials.UserName.UserName = "UserName";
        client.ClientCredentials.UserName.Password = "Pa**word";

        return client;
    }

然后您正常调用WebService

and then you call your WebService normaly

MyBasicConfiguredService().HellowWorld(input).getAwaiter().getResult();

每个人都快乐编码

这篇关于.net core 2和更高版本:具有NTLM授权的连接服务WcfServiceClient SOAP如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆