Click here to Skip to main content
12,623,776 members (29,278 online)
Click here to Skip to main content

Stats

363.6K views
6.5K downloads
194 bookmarked
Posted

Web Service Authentication

, 25 Nov 2009 CPOL
A simple mechanism to authenticate users to a WebService
src
Global.asax
WebServiceAuthentication.csproj.webinfo
bin
WebServiceAuthentication.dll
Web References
ServicePointReference
Reference.map
ServicePoint.disco
ServicePoint.wsdl
src_VS2005
Global.asax
App_Code
App_WebReferences
ServicePointReference
WebServiceAuthentication
ServicePointReference
ServicePoint.disco
ServicePoint.discomap
ServicePoint.wsdl
src_VS2010
App_Code
App_WebReferences
ServicePointReference
WebServiceAuthentication
ServicePointReference
ServicePoint.disco
ServicePoint.discomap
ServicePoint.wsdl
Global.asax
WebServiceAuthentication.suo
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;

namespace WebServiceAuthentication
{
	public class TestAuth : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label LabelSrv;
		protected System.Web.UI.WebControls.TextBox TextBoxService;
		protected System.Web.UI.WebControls.Label LabelUser;
		protected System.Web.UI.WebControls.Label LabelPwd;
		protected System.Web.UI.WebControls.TextBox TextBoxPwd;
		protected System.Web.UI.WebControls.TextBox TextBoxUserName;
		protected System.Web.UI.WebControls.Button ButtonGetToken;
		protected System.Web.UI.WebControls.Label LabelToken;
		protected System.Web.UI.WebControls.TextBox TextBoxToken;
		protected System.Web.UI.WebControls.Button ButtonService;
		protected System.Web.UI.WebControls.Button ButtonUseToken;
		protected System.Web.UI.WebControls.Label ServResponse;
		protected System.Web.UI.WebControls.Button ButtonAuth;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				//JUST FOR TEST: set the correct UserName & Password
				this.TextBoxUserName.Text="MyUserName";
				this.TextBoxPwd.Text="SeCrEt";
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.ButtonAuth.Click += new System.EventHandler(this.ButtonAuth_Click);
			this.ButtonService.Click += new System.EventHandler(this.ButtonService_Click);
			this.ButtonGetToken.Click += new System.EventHandler(this.ButtonGetToken_Click);
			this.ButtonUseToken.Click += new System.EventHandler(this.ButtonUseToken_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void ButtonAuth_Click(object sender, System.EventArgs e)
		{
			string ret;
			string UserName, Password, Key, ToHash;

			UserName=this.TextBoxUserName.Text;
			Password=this.TextBoxPwd.Text;
			DateTime dt = DateTime.Now;
			ToHash=UserName.ToUpper()+"|"+Password+"|"+dt.ToString("yyyyMMdd")+"|"+dt.ToString("HHmm");
			Key=Hash(ToHash)+"|"+UserName+"|I would like to log this string in a DB";

			ServicePointReference.ServicePoint Authenticate = new ServicePointReference.ServicePoint();
			ret=Authenticate.Authenticate(Key);
			if (ret==null)
				this.ServResponse.Text="NULL RECEIVED!!"; //never!
			else
			{
				this.ServResponse.Text="RECEIVED DATA: "+ret;
			}
		}

		private void ButtonGetToken_Click(object sender, System.EventArgs e)
		{
			string ret;

			ServicePointReference.ServicePoint Authenticate = new ServicePointReference.ServicePoint();
			ret=Authenticate.GetToken();
			this.TextBoxToken.Text=ret;
		}

		private void ButtonService_Click(object sender, System.EventArgs e)
		{
			string ret;
			string UserName, Password, ServiceName;
			string Key, ToHash;

			UserName=this.TextBoxUserName.Text;
			Password=this.TextBoxPwd.Text;
			ServiceName=this.TextBoxService.Text;
			DateTime dt = DateTime.Now;
			ToHash=UserName.ToUpper()+"|"+Password+"|"+dt.ToString("yyyyMMdd")+"|"+dt.ToString("HHmm");
			Key=Hash(ToHash)+"|"+UserName+"|I would like to log this string in a DB";

			ServicePointReference.ServicePoint Authenticate = new ServicePointReference.ServicePoint();
			ret=Authenticate.UseService(Key, ServiceName);
			this.ServResponse.Text=ret;
		}

		private void ButtonUseToken_Click(object sender, System.EventArgs e)
		{
			string ret;
			string UserName, Password, ServiceName, Token;
			string Key, ToHash;

			UserName=this.TextBoxUserName.Text;
			Password=this.TextBoxPwd.Text;
			ServiceName=this.TextBoxService.Text;
			Token=this.TextBoxToken.Text;
			ToHash=UserName.ToUpper()+"|"+Password+"|"+Token;
			Key=Hash(ToHash)+"|"+UserName+"|I would like to log this string in a DB";

			ServicePointReference.ServicePoint Authenticate = new ServicePointReference.ServicePoint();
			ret=Authenticate.UseService(Key, ServiceName);
			this.ServResponse.Text=ret;
		}

		private string Hash(string ToHash)
		{
			// First we need to convert the string into bytes, which means using a text encoder.
			Encoder enc = System.Text.Encoding.ASCII.GetEncoder();

			// Create a buffer large enough to hold the string
			byte[] data = new byte[ToHash.Length];
			enc.GetBytes(ToHash.ToCharArray(), 0, ToHash.Length, data, 0, true);

			// This is one implementation of the abstract class MD5.
			MD5 md5 = new MD5CryptoServiceProvider();
			byte[] result = md5.ComputeHash(data);

			return BitConverter.ToString(result).Replace("-", "").ToLower();
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

faina
Web Developer
Italy Italy
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161128.1 | Last Updated 25 Nov 2009
Article Copyright 2005 by faina
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid