Retrieve Identity username, email and other information in ASP.NET Core

access_time 4 years ago visibility25956 comment 0

Context

The identity system in ASP.NET has evolved over time. If you are using ASP.NET Core, you probably found User property is an instance of ClaimsPrincipal in Controller or Razor views. Thus to retrieve the information, you need to utilize the claims.

UserManager<IdentityUser<TKey>>

In the default MVC template provided by Visual Studio 2017, _LoginPartial.html view already includes the code to retrieve identity full user information.

@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
@{
     var user = await UserManager.GetUserAsync(User);
}

@if (SignInManager.IsSignedIn(User))
{
     <form asp-area="" asp-controller="Account" asp-action="Logout" method="post" id="logoutForm" class="navbar-right">
         <ul class="nav navbar-nav navbar-right">
             <li class="nav-item">
                 <a class="nav-link" asp-area="" asp-controller="Manage" asp-action="Index" title="Manage" data-toggle="dropdown"><i class="fa fa-user-o"></i>&nbsp;@(user.FullName ?? user.UserName)</a>
             </li>
             <li class="nav-item">
                 <button type="submit" class="btn btn-link nav-link"><i class="fa fa-sign-out"></i>&nbsp;Log out</button>
             </li>
         </ul>
     </form>
}
else
{
     <ul class="nav navbar-nav navbar-right">
         <li class="nav-item"><a class="nav-link" asp-area="" asp-controller="Account" asp-action="Login"><i class="fa fa-sign-in"></i>&nbsp;Log in</a></li>
         <li class="nav-item"><a class=" btn btn-primary" asp-area="" asp-controller="Account" asp-action="Register"><i class="fa fa-user-plus"></i>&nbsp;Register</a></li>
     </ul>
}

As coded above, UserManager.GetUserAsync method is used to retrieve my customized ApplicationUser (derived from IdentityUser<TKey>).

About @inject

As you can inject any services in your controller or services, you can also inject the services in your views using @inject.

For more details, please refer to https://docs.microsoft.com/en-us/aspnet/core/mvc/views/dependency-injection.

Use it in controllers

In your controllers, you can directly inject UserManager and then use it as you can do in the above Razor view example.

[Authorize]
     [Route("[controller]/[action]")]
     public class AccountController : Controller
     {
         private readonly UserManager<ApplicationUser> _userManager;
         private readonly SignInManager<ApplicationUser> _signInManager;
         private readonly IEmailSenderService _emailSender;
         private readonly ILogger _logger;

        public AccountController(
             UserManager<ApplicationUser> userManager,
             SignInManager<ApplicationUser> signInManager,
             IEmailSenderService emailSender,
             ILogger<AccountController> logger)
         {
             _userManager = userManager;
             _signInManager = signInManager;
             _emailSender = emailSender;
             _logger = logger;
         }

}

User property

Claims principle stores the identity information like user name and authentication type. The Claims property stores all the claims of the current user. Using those claims, we can authenticate based on any customization logic.

image

Summary

It is very convenient to retrieve user information by using Identity built in manager classes. You can also implement your own identity system and authentication flows by defining your own claim types and store management strategy.

info Last modified by Raymond at 4 years ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Want to publish your article on Kontext?

Learn more

Kontext Column

Created for everyone to publish data, programming and cloud related articles.
Follow three steps to create your columns.


Learn more arrow_forward

More from Kontext

Entity Framework Core Code-First - Generate Covering Index with Columns Included

local_offer entity-framework local_offer asp.net core local_offer Azure local_offer C#

visibility 183
thumb_up 1
access_time 4 months ago

In SQL Server or some other relational databases, it is a very common requirement to create covering index with columns included in index pages beside the index key columns. With Entity Framework Core, you can also easily generate covering indexes using purely C# code. For Kontext website, backend ...

local_offer identity core 2 local_offer ASP.NET

visibility 3019
thumb_up 0
access_time 6 years ago

In my previous post, I demonstrated how to migrate from ASP.NET Universal Membership Provider to ASP.NET Identity 2.1.0. https://kontext.tech/Blog/DotNetEssential/Archive/2015/1/31/migrate-from-systemwebproviders-to-aspnet-identity-10-and-then-to-210.html Based on the researches, I have ...

local_offer asp.net core local_offer asp.net core 3 local_offer C#

visibility 219
thumb_up 0
access_time 2 months ago

This page summarize information about how to retrieve client and server IP address in ASP.NET core applications.  Client IP address can be retrieved via HttpContext.Connection object. This properties exist in both Razor page model and ASP.NET MVC controller. Property  RemoteIpAddress ...

About column

Articles about ASP.NET Core 1.x, 2.x and 3.x.

rss_feed Subscribe RSS