Introduction:
In this article,i am going to explain about how to authenticate web service into a particular client machine without using
userid/password (stored in database).
Main:
Normally we know in windows/password based authentications we are using ip addess for validating windows account.
so here also i tried the same ip address approach for authenticating particular client machine.
Steps Involved,
1.Store Client machine ip in config file,
2.In Auth header i added extra field ‘ip’ for validating client machine ip address,
3.In client side,i added the extra coding for getting ip address using system.net namespace.
public class AuthHeader : SoapHeader
{
public string Username;
public string Password;
public string ip;
}
Web service sample code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace netprogWSex
{
/// <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")]
[WebMethod]
public string WelcomeString()
{
if (Authentication.ip == "124.123.129.119")
{
return "Welcome Client machine";
}
else
{
if (Authentication.Username == "test" && Authentication.Password == "test")
{
return "I am Web Method";
}
else
{
return "You don't have access";
}
}
//return "ends";
}
}
public class AuthHeader : SoapHeader
{
public string Username;
public string Password;
public string ip;
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; namespace netprogWSex { /// <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")] [WebMethod] public string WelcomeString() { if (Authentication.ip == "124.123.129.119") { return "Welcome Client machine"; } else { if (Authentication.Username == "test" && Authentication.Password == "test") { return "I am Web Method"; } else { return "You don't have access"; } } //return "ends"; } } public class AuthHeader : SoapHeader { public string Username; public string Password; public string ip; } } |
Client sample code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Net;
namespace WsClient
{
class Program
{
static void Main(string[] args)
{
ServiceReference2.AuthHeader authentication = new WsClient.ServiceReference2.AuthHeader();
authentication.Username = "";
authentication.Password = "";
string strhost = Dns.GetHostName();
string ipaddress1=null;
//IPAddress[] addr = Dns.GetHostByName(strhost);
IPHostEntry Local_Entry = Dns.GetHostByName(strhost);
IPAddress[] addr = Local_Entry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
ipaddress1 = addr[i].ToString();
}
authentication.ip = ipaddress1;
WsClient.ServiceReference2.WelcomeStringRequest obj = new WsClient.ServiceReference2.WelcomeStringRequest();
obj.AuthHeader = authentication;
WsClient.ServiceReference2.WelcomeStringResponse objres = new WsClient.ServiceReference2.WelcomeStringResponse();
WsClient.ServiceReference2.Service1SoapClient ong = new WsClient.ServiceReference2.Service1SoapClient();
Console.WriteLine(ong.WelcomeString(authentication));
Console.ReadLine();
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Net; namespace WsClient { class Program { static void Main(string[] args) { ServiceReference2.AuthHeader authentication = new WsClient.ServiceReference2.AuthHeader(); authentication.Username = ""; authentication.Password = ""; string strhost = Dns.GetHostName(); string ipaddress1=null; //IPAddress[] addr = Dns.GetHostByName(strhost); IPHostEntry Local_Entry = Dns.GetHostByName(strhost); IPAddress[] addr = Local_Entry.AddressList; for (int i = 0; i < addr.Length; i++) { ipaddress1 = addr[i].ToString(); } authentication.ip = ipaddress1; WsClient.ServiceReference2.WelcomeStringRequest obj = new WsClient.ServiceReference2.WelcomeStringRequest(); obj.AuthHeader = authentication; WsClient.ServiceReference2.WelcomeStringResponse objres = new WsClient.ServiceReference2.WelcomeStringResponse(); WsClient.ServiceReference2.Service1SoapClient ong = new WsClient.ServiceReference2.Service1SoapClient(); Console.WriteLine(ong.WelcomeString(authentication)); Console.ReadLine(); } } } |

Such a well written post.. Thnkx for sharing this post!
Wow this is a great resource.. I’m enjoying it.. good article
Thanks for the enthrilling article, can I ask where you get your sources from?
Properly, this really is my primary go to to your weblog! We are a group of volunteers and commencing a new initiative in a community in a similar niche. Your website supplied us useful details to function on. You’ve carried out a marvellous occupation!
Nice info, a lot of thanks towards the author. It really is incomprehensible to me now, but in basic, the usefulness and significance is overwhelming. Thanks again and very good luck!
Thank you all and you for this great website, and i will bookmark this post, I was curious about this for a long time now. Do you have a rss feed that I can add? Have a fantastic day!
Excellent post I must say.. Simple but yet interesting and engaging.. Keep up the awesome work!
found you on digg, darn happy I did, best post Ive read all day.
Keep posting stuff like this i really like it
Keep up the amazing work!! I love how you wrote this and I also like the colors here on this site. Very good opinions expressed here
this post is very usefull thx!
If you’re still on the fence: grab your favorite earphones, head down to a Best Buy and ask to plug them into a Zune then an iPod and see which one sounds better to you, and which interface makes you smile more. Then you’ll know which is right for you.