Click here to Skip to main content
15,880,796 members
Articles / Web Development / ASP.NET

Authentication for Web Services (using SOAP headers)

Rate me:
Please Sign up or sign in to vote.
4.63/5 (62 votes)
24 Jun 20032 min read 877.9K   14.7K   157  
Simple authentication for web services using SOAP headers.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace AuthForWebServices
{
	/// <summary>
	/// Summary description for WebService.
	/// </summary>
	public class WebService : System.Web.Services.WebService
	{
		public AuthHeader Authentication;

		public WebService()
		{
			//CODEGEN: This call is required by the ASP.NET Web Services Designer
			InitializeComponent();
		}

		#region Component Designer generated code
		
		//Required by the Web Services Designer 
		private IContainer components = null;
				
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if(disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);		
		}
		
		#endregion

		[SoapHeader ("Authentication", Required=true)]
		[WebMethod (Description="Returns some sample data")]
		public DataSet SensitiveData()
		{
			DataSet data = new DataSet();
			
			//Do our authentication
			//this can be via a database or whatever
			if(Authentication.Username == "test" && Authentication.Password == "test")
			{
				//they are allowed access to our sensitive data
				
				//just create some dummy data
				DataTable dtTable1 = new DataTable();
				DataColumn drCol1 = new DataColumn("Data", System.Type.GetType("System.String"));
				dtTable1.Columns.Add(drCol1);

				DataRow drRow = dtTable1.NewRow();
				drRow["Data"] = "Sensitive Data";
				dtTable1.Rows.Add(drRow);
				dtTable1.AcceptChanges();

				data.Tables.Add(dtTable1);


				
			}
			else
			{
				data = null;
			}			

			return data;
		}

	}

	public class AuthHeader : SoapHeader
	{
		public string Username;
		public string Password;
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
I've been programming for a few years now. I blog regularly at httpcode.

Comments and Discussions