Walter.BOM Namespace
Walter.Web.FireWall Namespace
ASP-WAF Web application firewall for .Net 6, .Net 5, .Net Standard 2.1 and .Net Core 3.x

GeoLocationMapping Class

Helper class that can be used to map country ISO code to a country as well as country to ISO codes.

Namespace:  Walter.BOM.Geo
Assembly:  Walter.BOM (in Walter.BOM.dll)

Syntax


public static class GeoLocationMapping

Remarks


This class contains quite a few utility classes that make it easy to validate is a GeoLocation is a given county or region and the association between regions and countries.

Perhaps the most central method is to convert 2letter ISO codes to GeoLocations TryGetValue(Nullable<(Of <<'(String>)>>), GeoLocation%). However the list does not stop there and take some time to go over the other methods available.

Examples


The bellow sample makes use of the County being of type GeoLocation. and uses that property to test if EU tax is applicable and if so validate if the VAT identifier is belonging to the company name provided. This sample uses the location provided by the firewall framework using NuGet package Walter.Web.FireWall This code is overly simplified but demonstrate how you could use this feature.

The method used to validate European value added tax (VAT) is from NuGet Package Walter.Vat and allows the validation of VAT data without making use of paid subscription services.

C#
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ProcessOrder(Order model)
{
    if (!ModelState.IsValid)
        return View(model);

    if (model.Country.IsInTheEuropeanUnion())
    {
        var vies = await Walter.Vat.EuropeanVatInformationQuery.GetAsync(model.VATNumber).ConfigureAwait(false);
        if (!vies.IsValid || !vies.CompanyName.Equals(model.CompanyName, StringComparison.OrdinalIgnoreCase))
        {
            if (!vies.IsValid)
            {
                if (vies.Exception is null)
                {
                    _logger?.Lazy().LogWarning(vies.Exception
                                    ,"Query European VAT failed with a for {vat} in {country}"
                                    , model.VATNumber
                                    , model.Country);
                }
                else
                {
                    _logger?.Lazy().LogWarning(vies.Exception,
                                    "Query European VAT failed with a {exception} exception error {message}"
                                    , vies.Exception.GetType().Name
                                    , vies.Exception.Message);
                }

                ModelState.AddModelError(nameof(model.VATNumber)
                                        , "The VAT number is not valid");
            }

            if (!vies.CompanyName.Equals(model.CompanyName, StringComparison.OrdinalIgnoreCase))
            {
                ModelState.AddModelError(nameof(model.VATNumber)
                                        , $"The VAT ID provided is belongs to another company than the one you specified, did you enter the wrong ID?");

                if (vies.CompanyName.Contains(model.CompanyName, StringComparison.OrdinalIgnoreCase))
                {
                    ModelState.AddModelError(nameof(model.CompanyName)
                                            , $"The VAT number provided is belongs to {vies.CompanyName}, this sounds like {model.CompanyName} but you should update it to avoid complications.");
                }
                else
                {
                    ModelState.AddModelError(nameof(model.CompanyName)
                                           , $"The VAT number provided is belongs to {vies.CompanyName} did you provide the wrong name?");
                }
            }

            return View(model);
        }
    }
}