0

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...

2
  • You shouldn't be surprised that all you find is outdated. Soap is outdated. Saying that as those stuff isn't anymore integral part of NET you seem to use something to get Soap/Webservice support. What do you use? Or do you have something written on your own but don 't show? Commented Jul 13, 2024 at 12:57
  • @Ralf this is my program.cs: using SoapCore; using OnlineDataExchange.Contracts; using OnlineDataExchange.Services; var builder = WebApplication.CreateBuilder(args); builder.Services.AddSingleton<IPinService, PinService>(); builder.Services.AddSoapCore(); builder.Services.AddControllers(); builder.Services.AddHttpContextAccessor(); var app = builder.Build(); app.UseSoapEndpoint<IPinService>("/SendPin.asmx", new SoapEncoderOptions()); app.UseAuthorization(); app.MapControllers(); app.Run(); Other than that, I don't have anything. Commented Jul 15, 2024 at 6:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.