Sample Project

Download this sample project configured with your Auth0 API Keys.

Rules are one of the most powerful features of Auth0. As JavaScript functions that execute each time a user authenticates, rules serve as middleware to extend the flexibility of your authentication flow. For a detailed description of rules, see the full documentation.

Create a Rule

To create a rule, go to the new rule page. You can create a new rule from scratch or use an existing template. These templates are written by Auth0 team to assist you complete common tasks.

Select the template called "Add country to the user profile"

Empty rule

This rule simply gets the country_name from the context and adds it as a new country attribute to the user profile.

Add country rule

This is just a starting template, and you can edit it to meet your business needs. Once you are done, save the rule and that's it. Whenever a user logs in, the rule will be executed, and the country will be added to the user's profile.

One thing to note is that for OIDC-conformant clients, all clams must be namespaced. So alter the code for the rule to namespace the claim:

function (user, context, callback) {
  if (context.request.geoip) {
    context.idToken[''] = context.request.geoip.country_name;

  callback(null, user, context);

This quickstart uses for the claim namespace, but it is suggested that you use a namespace related to your own Auth0 tenant for your claims, e.g

Add the country as a claim

The Auth0 middleware will not add the country as a claim, so you will need to do this manually. You can alter the middleware registration in the Startup class to add an OnAuthenticated event handler which extracts the country from the User object and add it as a claim.

var options = new Auth0AuthenticationOptions()
    Domain = auth0Domain,
    ClientId = auth0ClientId,
    ClientSecret = auth0ClientSecret,

    Provider = new Auth0AuthenticationProvider
        OnAuthenticated = context =>
            // Get the user's country
            JToken countryObject = context.User[""];
            if (countryObject != null)
                string country = countryObject.ToObject<string>();

                context.Identity.AddClaim(new Claim("country", country, ClaimValueTypes.String, context.Connection));

            return Task.FromResult(0);

Display the country in the user profile

Now that the user's country is returned by Auth0, you can display this inside the user profile page created in the previous step.

Update the UserProfileViewModel and add a Country property:

public class UserProfileViewModel
    public string Country { get; set; }

    public string EmailAddress { get; set; }

    public string Name { get; set; }

    public string ProfileImage { get; set; }

Update the Profile action in your AccountController to retrieve the country from the "country" claim:

public ActionResult Profile()
    var claimsIdentity = User.Identity as ClaimsIdentity;

    return View(new UserProfileViewModel()
        Name = claimsIdentity?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value,
        EmailAddress = claimsIdentity?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value,
        ProfileImage = claimsIdentity?.Claims.FirstOrDefault(c => c.Type == "picture")?.Value,
        Country = claimsIdentity?.Claims.FirstOrDefault(c => c.Type == "country")?.Value

And finally display the country in the profile view:

@model global::MvcApplication.ViewModels.UserProfileViewModel
    ViewData["Title"] = "User Profile";

<div class="row">
    <div class="col-md-12">
        <div class="row">

            <div class="col-md-2">
                <img src="@Model.ProfileImage"
                     alt="" class="img-rounded img-responsive" />
            <div class="col-md-4">
                    <i class="glyphicon glyphicon-envelope"></i> @Model.EmailAddress
                    <i class="glyphicon glyphicon-map-marker"></i> @Model.Country

Now when you run the application you will be able to see the user's country displayed:

Previous Tutorial
5. User Profile
Next Tutorial
7. Authorization
Use Auth0 for FREECreate free Account