Click here to Skip to main content
Click here to Skip to main content

Authentication for Web Services (using SOAP headers)

By , 24 Jun 2003
 

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
Web Developer
Australia Australia
Member
I've been programming for a few years now. I blog regularly at httpcode.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionreally good....memberssd_coolguy9 May '12 - 19:32 
hi dan,
ur artcle is really good... this clears my soapheader concept...
QuestionUsing SSL, this is pretty secure right?memberFredButters17 Apr '12 - 9:17 
If my web service host requires SSL, and I include username and password in the SOAP header, I can be confident that this whole process is secure, right?
 
Thanks. Good article, clean code.
QuestionAuthHeaderValuememberbacarndiaye28 Feb '12 - 5:54 
Hi,
I have follow your exemple but i don't but "AuthHeaderValue" it's no definit and i don't see in your code where you definit "AuthHeaderValue"
QuestionSoap header authenticationmemberdhanwin15 Feb '12 - 21:53 
Hi ,
 
I have a web service having authentication in soap header developed in Java from the client.. i have to pass the username and password from the asp site (C#.net) to access some functions from the web service.But i am getting the below error message.any one has the idea please help me.
 
error message:
 
Error cccour :com.sun.xml.wss.XWSSecurityException: com.sun.xml.wss.XWSSecurityException:
Message does not conform to configured policy [ AuthenticationTokenPolicy(S) ]: No Security Header found

 

with thanks,
Dhanesh.
GeneralMy vote of 5memberMember 803756728 Nov '11 - 18:01 
Very good article
GeneralMy vote of 4memberwwuwwei1 Oct '11 - 22:05 
good articles. thanks a lot
QuestionAuthentication for Web Services (using SOAP headers) in JAVAmemberMember 213261723 Aug '11 - 21:15 
Hi,
 
I needed to find a simple way to authenticate the users of web service.
The authentication approach should same as here but using different technology JAVA.
Can anybody suggest me about how to implement "Authentication for Web Services (using SOAP headers) in JAVA".
 
Thanks in advance.
QuestionUsing that "Authentication" object with PHP5.xmembercadburry17 Aug '11 - 5:20 
Hi!
 
Yep! Its possible to connect to my service via PHP, but im not able to use/pass the "Authentication" obect via PHP - has anybody tried this - PHP with this header authentication??
 
Thx
Cad
GeneralMy vote of 5membernipunasilva28 Jul '11 - 17:52 
Nice article, simple and informative
GeneralGood ArticlegroupGauravGupta21226 Sep '10 - 22:21 
Hii
 
Really great article .................
 
Gaurav

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 25 Jun 2003
Article Copyright 2003 by Dan_P
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid