Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have the following situation:
 
An android app is calling my c# webservice. In the HTTP header there will be a token which I have to check each time my webservice is called in order to validate the identity of the caller.
 
I would like to stress that my webservice is not a WCF service, its name ends with asmx.
 
The code for one of my webmethods looks like this:
 
[WebMethod]
      [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
      public List<Object> GetContact(string clientNr, int clientId = 0)
      {
          var contact = new List<Object>();
           ...code ommited
          
 
          return contact;
      }
 
My idea is to make a call like
[WebMethod]
      [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
      public List&lt;Object&gt; GetContact(string clientNr, int clientId = 0)
      {
          If(CheckToken())
          {
           var contact = new List&lt;Object&gt;();
           ...code ommited
           }
 
          return contact;
      }
 
The function CheckToken would return True or False. It would be reading the token from the HTTP Header, but how do I do that?
 
Kind regards
 
Jeroen
Posted 23-Jan-13 2:42am
Edited 23-Jan-13 4:08am
v3
Comments
digimanus at 23-Jan-13 9:49am
   
public List<Object> GetContact(string AuthTOKEN, string clientNr, int clientId = 0)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You will have to create the token derived from soapheader class.
public class TokenHeader : System.Web.Services.Protocols.SoapHeader
{
public string tokenNo;
}
 
in the web service class use
public TokenHeader token;
 
[WebMethod]

[SoapHeader("token",Required=true)]
public string GetToken()
{
if (checkToken())
return token.tokenNo;
else
return "Error in authentication";
}
 
private bool checkToken()
{

if (token!= null)
{
if (token.tokenNo==100)
return true;
else
return false;
}
else
return false;
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This is what I came up with:
namespace TestServiceHttpAuthentication
{
 
public class AuthenticateRequestHttpModule : IHttpModule
{
 
private HttpApplication mHttpApp;
 

 
public void Init(HttpApplication httpApp)
{
 
this.mHttpApp = httpApp;
 
mHttpApp.AuthenticateRequest += new EventHandler(OnAuthentication);
 
}
 

void OnAuthentication(object sender, EventArgs a)
{
HttpApplication application = (HttpApplication)sender;
 

HttpRequest request = application.Context.Request;

//WindowsIdentity identity = (WindowsIdentity)application.Context.User.Identity;
 

 
StreamWriter sw = new StreamWriter(request.MapPath("Test.txt"), true);
 
sw.WriteLine(application.Context.Request.HttpMethod.ToString());
sw.WriteLine(application.Context.Request.Headers.Count.ToString());
NameValueCollection coll;
coll=application.Context.Request.Headers;
string[] arr1 = coll.AllKeys;
for(int loop1=0;loop1<arr1.length;loop1++)>
{
sw.WriteLine(arr1[loop1].ToString());
 
string[] arr2=coll.GetValues(arr1[loop1]);
for(int loop2=0;loop2<arr2.length;loop2++)>
{
sw.WriteLine(arr2[loop2].ToString());
}
}
 
sw.Flush();
 
sw.Close();
 

 

 
}
 

 
public void Dispose()
 
{ }
 
}
 
}
 
And in the web.config:
<httpModules>
        add name="AuthenticateRequestHttpModule" type="TestServiceHttpAuthentication.AuthenticateRequestHttpModule, TestServiceHttpAuthentication" />
 

Now I can read from the HTTP header, validate the values from specific key/value pairs.
 
Thanks for those who've thought with me.
 
Jeroen
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 330
1 Sergey Alexandrovich Kryukov 279
2 BillWoodruff 245
3 Afzaal Ahmad Zeeshan 215
4 CPallini 205
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100