I’m currently working on integrating a SOAP service into my C# application and I’m having trouble implementing authentication using SOAP headers. I just want to read the username and password from the SOAP request. I am testing on SoapUI.
Here is the request structure:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:onl="http://schemas.datacontract.org/2004/07/OnlineDataExchange">
<soapenv:Header>
<wsse:Security>
<wsse:UsernameToken>
<wsse:Username>SOAP_ODE</wsse:Username>
<wsse:Password>123456</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<tem:SendPin>
<tem:request>
<onl:TransactionID>?</onl:TransactionID>
<onl:MSISDN>?</onl:MSISDN>
</tem:request>
</tem:SendPin>
</soapenv:Body>
</soapenv:Envelope>
This is my code:
SendPinRequest.cs:
using System.Runtime.Serialization;
namespace OnlineDataExchange
{
[DataContract]
public class SendPinRequest
{
[DataMember(Order = 1, IsRequired = true)]
public string TransactionID { get; set; }
[DataMember(Order = 2, IsRequired = true)]
public string MSISDN { get; set; }
}
}
IPinService.cs:
using System.ServiceModel;
using System.Threading.Tasks;
namespace OnlineDataExchange.Contracts
{
[ServiceContract]
public interface IPinService
{
[OperationContract]
Task<SendPinResponse> SendPin(SendPinRequest request);
}
}
PinService.cs:
using OnlineDataExchange.Contracts;
using SoapCore;
namespace OnlineDataExchange.Services
{
public class PinService : IPinService
{
private readonly IConfiguration _configuration;
public PinService(IConfiguration configuration)
{
_configuration = configuration;
}
public async Task<SendPinResponse> SendPin(SendPinRequest request)
{
string resp_code = "00";
???? // Here I want to retreive the username and password from the SOAP Header
if (request == null) throw new ArgumentNullException(nameof(request));
// Check MSISDN
if (resp_code == "00") resp_code = ValidationUtils.ValidateMSISDN(request.MSISDN);
// Check Password
if (resp_code == "00") resp_code = ValidationUtils.ValidatePassword(password);
// Check credentials
Authentication au = new Authentication(_configuration);
if (resp_code == "00") resp_code = au.AuthenticateUser(username, password);
return new SendPinResponse
{
TransactionID = request.TransactionID,
ResponseCode = resp_code,
ResponseText = ResponseCodes.ResponsesDictionary[resp_code],
MSISDN = request.MSISDN
};
}
}
}
I tried a lot of solutions found on google but none worked and they all seemed outdated…