RickardPettersson / swish-api-csharp

Swish For Merchant API Client .Net Standard Library
MIT License
28 stars 13 forks source link

Problem with Certificates on Azure Web App #27

Open xxandeer opened 11 months ago

xxandeer commented 11 months ago

I have built a web app that functions perfectly in my local environment. However, when I deploy it to Azure as a web app, I encounter issues with certificates. It seems that Azure is not recognizing the certificates in the .p12 format. I am considering a simple solution: renaming the certificate files from .p12 to .pfx and uploading them in the "Certificates" section on Azure. Then, I plan to utilize FindType.FindByThumbprint to load and use these certificates.

Has anyone faced a similar issue or implemented a solution for it?

Your insights and suggestions on how to resolve this issue would be greatly appreciated. Thank you!

xxandeer commented 11 months ago

I get this error on var response = payoutClient.MakePayoutRequest response = null

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=SwishApi StackTrace: at SwishApi.Models.PayoutRequestEnvelope.buildSignature(ClientCertificate signingCertificate)

`public async Task OnPostAsync() { // Obtain certificates from Azure Key Vault var certificateOptions1 = new DownloadCertificateOptions("TestCertificate"); var response1 = await keyVaultClient.DownloadCertificateAsync(certificateOptions1); var certificate1 = response1.Value;

var certificateOptions2 = new DownloadCertificateOptions("TestSigningCertificate");
var response2 = await keyVaultClient.DownloadCertificateAsync(certificateOptions2);
var certificate2 = response2.Value;

using var certificateStream1 = new MemoryStream(certificate1.RawData);
using var certificateStream2 = new MemoryStream(certificate2.RawData);

var clientCertificate1 = new SwishApi.Models.ClientCertificate()
{
    CertificateAsStream = certificateStream1,
    Password = "swish",
};

// Set up your Swish payout client
var payoutClient = new SwishApi.PayoutClient(clientCertificate1, "https://eofvqci6optquip.m.pipedream.net", "1234", "1234679304", true, SwishApi.Environment.Emulator);

string instructionUUID = Guid.NewGuid().ToString("N").ToUpper();

var clientCertificate2 = new SwishApi.Models.ClientCertificate()
{
    CertificateAsStream = certificateStream2,
    Password = "swish",
};

// Make a payout request with the Swish payout client
var response = payoutClient.MakePayoutRequest(
    PayoutTo,
    Personnummer,
    Amount,
    Message,
    instructionUUID,
    "7d70445ec8ef4d1e3a713427e973d097",
    clientCertificate2
);`
xxandeer commented 11 months ago

I've got it to work here is my solution, Ofc. cert should be loaded on program start.

` public PayoutModel() { // Initialize the SecretClient here. Replace with your actual Key Vault URL. keyVaultClient = new SecretClient(new Uri("https://your-key-vault.vault.azure.net/"), new DefaultAzureCredential()); }

public async Task OnPostAsync() { // Ladda ner certifikaten som secrets var secretBundle1 = await keyVaultClient.GetSecretAsync("TestCertificate"); var secretBundle2 = await keyVaultClient.GetSecretAsync("TestSigningCertificate");

// Skapa MemoryStream-objekt från secrets
using var certificateStream1 = new MemoryStream(Convert.FromBase64String(secretBundle1.Value.Value));
using var certificateStream2 = new MemoryStream(Convert.FromBase64String(secretBundle2.Value.Value));

var clientCertificate1 = new SwishApi.Models.ClientCertificate()
{
    CertificateAsStream = certificateStream1
};

var clientCertificate2 = new SwishApi.Models.ClientCertificate()
{
    CertificateAsStream = certificateStream2
};

// Set up your Swish payout client
var payoutClient = new SwishApi.PayoutClient(clientCertificate1, "https://eofvqci6optquip.m.pipedream.net", "1234", "1234679304", true, SwishApi.Environment.Emulator);

string instructionUUID = Guid.NewGuid().ToString("N").ToUpper();

// Make a payout request with the Swish payout client
var response = payoutClient.MakePayoutRequest(
    PayoutTo,
    Personnummer,
    Amount,
    Message,
    instructionUUID,
    "7d70445ec8ef4d1e3a713427e973d097",
    clientCertificate2
);;`
RickardPettersson commented 11 months ago

nice work, thanks to share solution.