ASP.NET Core v1.1: User Profile

View on Github

ASP.NET Core v1.1: User Profile

Gravatar for
By Damien Guard

This tutorial will show you how to get the user's profile and display it. We recommend you to Log in to follow this quickstart with examples configured for your account.

I want to explore a sample app

2 minutes

Get a sample configured with your account settings or check it out on Github.

View on Github
System requirements: .NET Core SDK 1.1.0 | ASP.NET Core 1.1.1 | Microsoft Visual Studio 2017 Update 3 or .NET core command line tools

Pros and cons

Getting the Profile

As mentioned in the previous step, the OIDC middleware will automatically extract the user's information from the ID Token and add it as claims to the ClaimsIdentity.

The seed project contains a controller action and view which will display the claims associated with a particular user. Once a user has signed in, you can simply go to /Account/Claims to see these claims.

You may also want to create a nicer looking user profile page which will display a user's name, email address and profile image.

First create view model which will contain the basic user profile information, such as a Name, EmailAddress and ProfileImage:

Add a new Profile action to the AccountController and extract the relevant claims and add them to a new instance of UserProfileViewModel which is then passed to the view. Be sure to decorate the action with the [Authorize] attribute so only authenticated users can access the action:

The User.Identity.Name property will look for a claim of type on the user object. Auth0 passes the name of the user in the name claim of the ID Token, but this does not get automatically matched the claim type, and therefore User.Identity.Name will return null.

There is however a way to control the claim type which ASP.NET Core should retrieve when accessing the name through User.Identity.Name. To do this you need to update the OIDC middleware registration in the Startup class and set the NameClaimType of the TokenValidationParameters property. By setting this value to name, ASP.NET Core will retrieve the value of the name claim which was passed in the ID Token whenever you access the name of the user using the User.Identity.Name property.

You will also need to update the list of scopes to ensure that your request the profile scope. This will ensure the user's profile information is returned as claims in the ID Token.

Next create a view. For the view, display a user profile card at the top with the user's name, email address and profile image.

Now when you log in and then go to the URL /Account/Profile you will see all the user's profile displayed.

Universal Login with Auth0

Displaying the User's Name in the Navigation Bar

You may also want to put a link in the top navigation bar to display the user's name, and when the user clicks on that you can navigate them to their Profile page.

Go to the Views/Shared/_Layout.cshtml file and update the Navbar section which displays the Login and Logout options to also display the user's name and link to the Profile action in the AccountController:

Now, after the user has signed it you will be able to see the user's name in the top right corner of the Navbar:

Use Auth0 for FREE