For websites that need to display different content for users in different countries and regions, we usually identify the user's location based on the IP address. It means we have to write our own code, purchasing and maintaining an IP database, or using a third-party IP data service, it will take huge efforts. Although a complete IP database can identify all countries and regions, in many cases, what the website wants is to distinguish a few countries and regions, not all of them. How to quickly do it without purchasing an IP database and write a lot of code? Let's see how Azure can help us with that.
Azure Front Door Again
I have introduced Azure Front Door in a couple of posts.
- A Brief Introduction for Azure Front Door
- Solving Azure AD Sign In Failure with Azure Front Door
- Configure URL Redirect on Azure Front Door
- Load Balancing My Blog on Azure
- How to Prevent Access to App Service Url Behind Azure Front Door
This time, I'll still use Azure Front Door to identify users from China as an example.
Custom Rule Engine
Go to "Rules engine configuration" in Front Door blade. Click "Add"
Give it a name, e.g. in my case "Moonglade1055", this engine can have multiple rules under it. Click "Add rule", named e.g. "MarkChinaRequests". We'll match user regions based on IP address.
Click "Add a condition", choose "Remote address", "Geo Match", then select "China" and "Hong Kong", "Taiwan" because they are a part of China.
Click "Add an action", choose "Request header", "Append". We'll add a custom HTTP header named "geo-match", value as "china" for those requests.
After saving the rule, we can see our custom rule engine under the list, click "Associate routing rule".
Select the routing rules that our website traffic will go through.
Save the changes and wait for a few minutes for Front Door to take effect.
Identify User Region in Website
Take ASP.NET Core as an example, we can read "geo-match" value from HTTP header to get the value that Azure Front Door assigned to the request.
Request.Headers["geo-match"]
Now, we can show China only content to China users. The website doesn't need to have code that query an IP database to determine user's location. Azure saved us a lot of effort!
Martin
Genius, GREAT EXPLANATION!