How to create a webservice with basic authentication

Here I would like to explain that How to create a webservice with basic authentication or how basic authentication works in webservice , asp.net c#.And How to pass credentials to a WebService that Uses Basic Authentication . calling-web-services-using-basic-authentication.When you select the Basic authentication check box in IIS , it will deny access to incoming requests which do not includes security credentials[domain, user name, and password]. So if you are working with Basic authentication and webservices the below given example will help you.

Here I am explaining how to do authentication in webservices , we have to take care of Before invoking a method on a Web service that uses Basic authentication, the client needs to initialize the Credentials property of the wrapper class with a NetworkCredentials object and then call the method

steps.
1. create a webservice with the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace TestWebService
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
//public AuthHeader Authentication;
//[SoapHeader("Authentication",Required = true)]
[WebMethod]
public string HelloWorld()
{
string strResult = null;

strResult = "Hello World";

return strResult;
}

[System.Web.Services.WebMethod()]
public double FahrenheitToCelsius(double Fahrenheit)
{
return ((Fahrenheit - 32) * 5) / 9;
}
}
}



2. Create the proxy class for this service . and use it in your consuming application. Once the proxy class has been generated add the following code in your proxy class to override the GetWebRequest method.

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");
}
}
return request;
}



3. Create the consuming application with the following code.

ICredentials creds;
creds = new NetworkCredential("username", "password", "domain");
wsConvert.Credentials = creds;
//Optional:
wsConvert.PreAuthenticate = true;
double temperature =
System.Convert.ToDouble(TemperatureTextbox.Text);
FahrenheitLabel.Text = "Fahrenheit To Celsius = " +
wsConvert.FahrenheitToCelsius(temperature).ToString();

Leave a Reply

Your email address will not be published. Required fields are marked *