ASP.NET Core Rules

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 log in, the rule will be executed, and the country will be added to the user's profile.

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:

[Authorize]
public IActionResult Profile()
{
  return View(new UserProfileViewModel()
  {
    Name = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value,
    EmailAddress = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value,
    ProfileImage = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value,
    Country = User.Claims.FirstOrDefault(c => c.Type == "country")?.Value
  });
}

And finally display the country in the profile view:

@model SampleMvcApp.ViewModels.UserProfileViewModel
@{
  ViewData["Title"] = "User Profile";
}

<div class="row">
  <div class="col-md-12">
    <div class="row">
      <h2>@ViewData["Title"].</h2>

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

Ensure the Country scope is requested

You will also need to ensure that you request the country scope. This will ensure that the country claim is returned in the id_token. Go back to the Configure method of the Startup class and update the registration of the OIDC middleware to request the country scope:

var options = new OpenIdConnectOptions("Auth0")
{
    // Code omitted for brevity...
};
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("name");
options.Scope.Add("email");
options.Scope.Add("picture");
options.Scope.Add("country");
app.UseOpenIdConnectAuthentication(options);

Run the application

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

Previous Tutorial
6. User Profile
Next Tutorial
8. Authorization
Use Auth0 for FREECreate free Account