5,447,640 members and growing! (20,669 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Authentication for Web Services (using SOAP headers)

By Dan_P

Simple authentication for web services using SOAP headers.
C#, Windows, .NET 1.0, .NET 1.1, .NET, Visual Studio, ASP.NET, Dev

Posted: 24 Jun 2003
Updated: 24 Jun 2003
Views: 174,889
Bookmarked: 78 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
34 votes for this Article.
Popularity: 5.83 Rating: 3.81 out of 5
4 votes, 12.1%
1
1 vote, 3.0%
2
5 votes, 15.2%
3
10 votes, 30.3%
4
13 votes, 39.4%
5

Sample Image - AuthForWebServices.gif

Introduction

I recently put up a few web services for a client of mine, which returned some sensitive data. I needed to find a simple way to authenticate the users of these web services. This is the approach I took.

Background

I've started using web services fairly often in the applications that I've been developing, in most cases the information they pass is suitable for the public domain. However a recent project forced me to look into different authentication methods.

My requirements were that, it had to be simple for the client applications to authenticate, also that the web based administration system had to be used. This prevented me from using the Windows authentication (which is fairly easy to use for the clients of this web service.) By using SOAP headers to pass username and password information, it greatly simplifies any authentication request.

Using the code

I wanted to make it really easy for the client to understand:

protected System.Web.UI.WebControls.DataGrid dgData;
    
private void Page_Load(object sender, System.EventArgs e)
{
    //simple client

    AuthWebService.WebService webService = new AuthWebService.WebService();
    AuthWebService.AuthHeader authentication = new 
                              AuthWebService.AuthHeader();

    authentication.Username = "test";
    authentication.Password = "test";
    webService.AuthHeaderValue = authentication;

    //Bind the results - do something here

    DataSet dsData = webService.SensitiveData();

    dgData.DataSource = dsData;
    dgData.DataBind();    

}

Basically all the client needs to do is create an authentication object, fill out the username and password, then pass them to the web service object. The web service code is also pretty simple, the .NET framework lets you create custom SOAP headers by deriving from the SoapHeader class, so we wanted to add a username and password:

using System.Web.Services.Protocols;

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

The next step is to identify the web services that need the authentication, in the example I've included it's the method SensitiveData. To force the use of our new SOAP header we need to add the following attribute to our method:

[SoapHeader ("Authentication", Required=true)]

So our full definition for our web service method is:

public AuthHeader Authentication;


[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;
}

I should also mention that when I say SOAP headers, I actually mean the soap:Header element in a SOAP request, it has nothing to do with the HTTP headers sent with the request. The SOAP request looks something like:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AUTHHEADER xmlns="http://tempuri.org/">
      <USERNAME>string</USERNAME>
      <PASSWORD>string</PASSWORD>
    </AUTHHEADER>
  </soap:Header>
  <soap:Body>
    <SENSITIVEDATA xmlns="http://tempuri.org/" />
  </soap:Body>
</soap:Envelope>

I've included both the client and the web service in the attachment.

History

  • 25/06/2003 - Article created

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

About the Author

Dan_P


I've been programming for a few years now. I've been working extensively with the .NET framework for the past two years. I founded my own development / consulting company - httpcode which has been lucky enough to work on some pretty cool things.
Freelance Programmer - Httpcode
Occupation: Web Developer
Location: Australia Australia

Other popular C / C++ Language articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 32 (Total in Forum: 32) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionWeb service security techniquesmemberJagadeeshs4:54 4 Sep '08  
GeneralWeb service securitymemberJagadeeshs4:49 4 Sep '08  
Generalcall from oracle appsmemberanjali.8620:08 27 Aug '08  
GeneralCall from another languagememberMauricio_Junior10:32 7 Apr '08  
QuestionObject reference not set to an instance of an objectmemberCraig_L0:28 7 Aug '07  
Generalhow to send this tricky header? [modified]memberblackjack21504:06 21 Feb '07  
GeneralRe: how to send this tricky header?memberLakhan Singh Rathore18:32 5 Jul '07  
QuestionMore Web Services,What Client Authenticationmemberkenzhen1:06 31 Oct '06  
GeneralAuthorization does not = Authentication - Weakmemberpunsu10:53 17 Aug '06  
Question.net client and non .net WSmemberwakewakeup0:13 16 Jun '06  
AnswerRe: .net client and non .net WSmemberpunsu9:44 23 Aug '06  
QuestionAuthentication object never instantiated in Messages.asmxsussAnonymous22:22 9 Oct '05  
GeneralAn unhandled exception of type 'System.Net.WebException' occurred in system.web.services.dllmemberKhoiNguyen13:54 30 Aug '05  
GeneralAn even simplier option.memberDanielHac16:45 2 Mar '05  
GeneralWSE 2.0memberdavidchentw@gmail.com20:08 21 Feb '05  
GeneralPassword EncryptionmemberTrickUK7:33 29 Jan '04  
GeneralRe: Password Encryptionmembersorcerer_842:32 8 May '08  
GeneralSoapHeader(..., Required = true)memberlapierrem9:20 25 Sep '03  
GeneralRe: SoapHeader(..., Required = true)memberWillemM21:28 26 Dec '03  
GeneralRe: SoapHeader(..., Required = true)memberjayprakash314:29 17 Jul '07  
GeneralSensitive data is not encrypted???memberGunmen12:56 26 Jun '03  
GeneralRe: Sensitive data is not encrypted???memberDan_P15:03 26 Jun '03  
GeneralRe: Sensitive data is not encrypted???memberAK17:09 23 Jul '03  
GeneralRe: Sensitive data is not encrypted???membernap2k0:05 12 Feb '04  
GeneralPassword...membermikasa8:16 26 Jun '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Jun 2003
Editor: Smitha Vijayan
Copyright 2003 by Dan_P
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project