Basic Authentication in Asp.net Core
See original GitHub issueI 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:
- Created 7 years ago
- Comments:8 (4 by maintainers)

Top Related StackOverflow Question
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
Perfectly. Thanks a lot.