I have the following controller class for my web service. I am trying to add authentication to it using SoapHeader. The system is using .NET 4.0. My code looks like:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.IO;
using System.Web.Services.Protocols;
public class AuthHeader : System.Web.Services.Protocols.SoapHeader
{
public string username { get; set; }
public string password { get; set; }
public bool IsValid()
{
return this.username == "admin" && this.password == "admin";
}
}
[WebService(Namespace = "http://tempuri.org")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[ScriptService]
public class FormController: System.Web.Services.WebService
{
public AuthHeader auth;
[WebMethod]
[SoapHeader ("auth")]
public string GetFormTypes()
{
if (auth != null)
{
if (auth.IsValid()) {
var obj = SQLQueries.ParseQuery(false, "select * from form");
Debug.WriteLine(obj);
obj.WriteToResponse();
return "Successfully authenticated";
}
else {
var res = "Invalid credentials";
return res;
}
}
else
{
var res = "Error in authentication";
return res;
}
}
}
I am testing it using postman tool. My payload body looks 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>admin</username>
<password>admin</password>
</AuthHeader>
</soap:Header>
<soap:Body>
<GetFormTypes xmlns="http://tempuri.org" />
</soap:Body>
</soap:Envelope>
All examples that I checked online including Microsoft's official docs do it in a similar way yet my code does not work. When I send the request, the value of soap header auth is always null.
What am I doing wrong ?
