question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Basic Authentication in Asp.net Core

See original GitHub issue

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("xx", "xx","xx"); string result = client.HelloWorld(); In Asp.net Core, I tried WCF Connected Service Visual Studio Extension, and it generate the client code correctly. But I failed to call the web service method due to that unauthenticated. I tried a lot of ways, but nothing works. ` 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;`.

In addition, I could inherit generated client code in mvc, but it does not exist in Asp.net Core generated code. ` public class WebServiceEx:BasicWebService.WebService1 { protected override System.Net.WebRequest GetWebRequest(Uri uri) {

        HttpWebRequest request;
        request = (HttpWebRequest)base.GetWebRequest(uri);

        //if (PreAuthenticate)
        //{
        //NetworkCredential networkCredentials = Credentials.GetCredential(uri, "Basic");
        //if (networkCredentials != null)
        //{

        //    byte[] credentialBuffer = new UTF8Encoding().GetBytes(networkCredentials.UserName + ":" + networkCredentials.Password);

        //    request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);
        //}
        //else
        //{
        //    throw new ApplicationException("No network credentials");
        //}
        //}
        byte[] credentialBuffer = new UTF8Encoding().GetBytes("xx"+ ":" + "xx");

        request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);
        return request;
    }

}`

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

12reactions
hongdaicommented, Mar 21, 2017

Assume you use EndpointConfiguration.WebService1Soap, you need to change following code, I bolded the lines need modification: private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration) { if ((endpointConfiguration == EndpointConfiguration.WebService1Soap)) { 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; }

public WebService1SoapClient(EndpointConfiguration endpointConfiguration) : base(WebService1SoapClient.GetBindingForEndpoint(endpointConfiguration), WebService1SoapClient.GetEndpointAddress(endpointConfiguration)) { this.Endpoint.Name = endpointConfiguration.ToString(); this.ChannelFactory.Credentials.UserName.UserName = “yourusername”; this.ChannelFactory.Credentials.UserName.Password = “yourpassword”; ConfigureEndpoint(this.Endpoint, this.ClientCredentials); }

Do you need to use EndpointConfiguration.WebService1Soap12?

Thanks, Hong

0reactions
Edward-Zhoucommented, Mar 23, 2017

Perfectly. Thanks a lot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic Authentication in ASP.NET Web API
Basic authentication is performed within the context of a "realm." The server includes the name of the realm in the WWW-Authenticate header. The ......
Read more >
Basic Authentication in ASP.NET Core - Software Engineering
The basic authentication can be validated in the authorize attribute using the correct scheme.
Read more >
What is Basic authentication and how to add it in ASP.NET ...
Basic authentication is a way for a web browser to provide a username and password when making a HTTP request. The way it...
Read more >
NET 6.0 - Basic Authentication Tutorial with Example API
In this tutorial we'll go through a simple example of how to implement custom Basic HTTP authentication in a .NET 6.0 API with...
Read more >
ASP.NET Web API Basic Authentication
The ASP.NET Web API Basic Authentication is performed within the context of a “realm.” The server includes the name of the realm in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found