Apple .WellKnown file management for Azure App and React

by Brett Andrew
17/10/2023

We had to overcome a small challenge today, so thought I would share an elegant and simple way to overcome this.

We have a React App, hosted on the same server as our API service.

In order to add an Apple Pay certificate at a fixed address of:

/.well-known/apple-developer-merchantid-domain-association

and also cater for being a multi-tennant setup (the react app loads data based on the URL it arrived in at). We needed to do some web.config magic for this file and manage it through our APIs.

The Helper class is our own written .net class and the controller for Website was already setup also, but you should be able to use the same logic.

Web.Config Code

First the web.config setting, if your React app is hosted on an Azure App service, then you can adjust the web.config file to rewrite the URL, this is important as Apple will detect a redirect and wants to see this file come directly from the same call.

This will look for the specific /.well-known... file and if found rewrite the URL to collect the data from your API. In our case we had an API controller setup, so we only had to create the function to return the file contents.

Change the API location to something that suits you.

Our API is located here:
/api/Website/GetWellKnown

<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<system.webServer>
		<rewrite>
			<rules>
				<rule name="Redirect well-known" stopProcessing="true">
					<match url="^\.well-known/apple-developer-merchantid-domain-association$" />
					<action type="Rewrite" url="/api/Website/GetWellKnown" />
				</rule>
			</rules>
		</rewrite>
	</system.webServer>
</configuration>
.NET API to send file contents back.


Next we have to write the code for the .net API. We have a function where we can store next data and retrieve easily, this retrieves the data directly out of the clients database, allowing each client to have a unique value.

If you need to cater for multiple domain names, you can use a switch command and the request Request.Host value to return the correct file or data.

        //used to replicate the /.well-known/apple-developer-merchantid-domain-association used for Apple Stripe pay
        //under payment settings, users can set the contents of this file manually
        [Produces("text/plain")]
        [HttpGet("[action]")]
        public async Task GetWellKnown()
        {
            IHelper Helper = (IHelper)HttpContext.RequestServices.GetService(typeof(IHelper));
            var fileContents = Helper.GetSystemSetting("AppleWellKnownFile");
            Response.Clear();
            Response.ContentType = "text/plain";
            await Response.WriteAsync(fileContents);
            await Response.Body.FlushAsync();
            
        }

There is a hidden discussion on this page, register to see or write comments.

I hope that helps! Leave a comment if it helped you or if you have any questions or need a consultant!

Brett Andrew

Enterprise Architect / Lead Developer / Director

Formition Pty Ltd

Contact us

popupimage

SaaS Developer Essentials

4 years into building our own SaaS product, here is what we wish we knew at the start.

Read More

Powered by mition