Search code examples
web-serviceswcfasp.net-coreasmx

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


I created a Asmx Web service and host it in IIS, in MVC, I could call it from below code:

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

But, I failed to mark it work under Asp.net Core. Here is the code what I have tried.

 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;

Solution

  • Got solution from GitHub, thanks for the suggestion from hongdai. Modify the generated reference.cs with below:

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

    Reference:https://github.com/dotnet/wcf/issues/1812