Asp.net 核心 Web 应用程序中的 Asmx Web 服务基本身份验证

Asmx Web Service Basic Authentication in Asp.net Core Web Application

我创建了一个 Asmx Web 服务并将其托管在 IIS 中,在 MVC 中,我可以从下面的代码中调用它:

        BasicWebService.WebService1 client = new BasicWebService.WebService1();
        client.Credentials = new System.Net.NetworkCredential("user", "pwd","domain");
        string result = client.HelloWorld();

但是,我未能将其标记为在 Asp.net Core 下工作。 这是我试过的代码。

 ServiceReference1.WebService1SoapClient client = new ServiceReference1.WebService1SoapClient(ServiceReference1.WebService1SoapClient.EndpointConfiguration.WebService1Soap);
        client.ClientCredentials.UserName.UserName = "xx";
        client.ClientCredentials.UserName.Password = "xx";   

        //string USER = "xx";
        //string PASSWORD = "xx";
        //string Domain = "xx";
        //NetworkCredential netCredential = new NetworkCredential(USER, PASSWORD,Domain);
        ////client.Credentials = new System.Net.NetworkCredential("xx", "xx", "xx");
        //client.ClientCredentials.Windows.ClientCredential = netCredential;// netCredential.GetCredential(new Uri("http://localhost/WCFBasicSecurity/WebService1.asmx"), "Basic");
        ServiceReference1.HelloWorldResponse result =client.HelloWorldAsync().Result;

从GitHub得到解决方案,感谢hongdai的建议。使用以下内容修改生成的 reference.cs:

public WebService1SoapClient(EndpointConfiguration endpointConfiguration) : 
        base(WebService1SoapClient.GetBindingForEndpoint(endpointConfiguration), WebService1SoapClient.GetEndpointAddress(endpointConfiguration))
{
    this.Endpoint.Name = endpointConfiguration.ToString();
    this.ChannelFactory.Credentials.UserName.UserName = "xx\xx";
    this.ChannelFactory.Credentials.UserName.Password = "xxxx";
    ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
    if ((endpointConfiguration == EndpointConfiguration.WebService1Soap))
    {

        //Transport Security
        System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding(System.ServiceModel.BasicHttpSecurityMode.Transport);
        result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Basic;            
        result.MaxBufferSize = int.MaxValue;
        result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
        result.MaxReceivedMessageSize = int.MaxValue;
        result.AllowCookies = true;
        return result;
    }
    if ((endpointConfiguration == EndpointConfiguration.WebService1Soap12))
    {
        System.ServiceModel.Channels.CustomBinding result = new System.ServiceModel.Channels.CustomBinding();
        System.ServiceModel.Channels.TextMessageEncodingBindingElement textBindingElement = new System.ServiceModel.Channels.TextMessageEncodingBindingElement();
        textBindingElement.MessageVersion = System.ServiceModel.Channels.MessageVersion.CreateVersion(System.ServiceModel.EnvelopeVersion.Soap12, System.ServiceModel.Channels.AddressingVersion.None);
        result.Elements.Add(textBindingElement);
        System.ServiceModel.Channels.HttpTransportBindingElement httpBindingElement = new System.ServiceModel.Channels.HttpTransportBindingElement();
        httpBindingElement.AllowCookies = true;
        httpBindingElement.MaxBufferSize = int.MaxValue;
        httpBindingElement.MaxReceivedMessageSize = int.MaxValue;
        result.Elements.Add(httpBindingElement);
        return result;
    }
    throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}
    private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
    {
        if ((endpointConfiguration == EndpointConfiguration.WebService1Soap))
        {
            return new System.ServiceModel.EndpointAddress("https://localhost/WCFBasicSecurity/WebService1.asmx");
            //return new System.ServiceModel.EndpointAddress("http://localhost/WCFBasicSecurity/WebService1.asmx");
        }
        if ((endpointConfiguration == EndpointConfiguration.WebService1Soap12))
        {
            return new System.ServiceModel.EndpointAddress("http://localhost/WCFBasicSecurity/WebService1.asmx");
        }
        throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
    }

参考:https://github.com/dotnet/wcf/issues/1812