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 3 years ago * This page is subject to Site terms.

More from Kontext

Modern Web Application - Azure Blob Storage for Uploaded Files

local_offer Azure local_offer asp.net core local_offer dotnetcore

visibility 80
thumb_up 0
access_time 2 months ago

With cloud platforms like Azure, we can totally separate user content storage from web application storage to decouple components from each other and to make the application easy to scale and deploy. This article provides detailed information with code snippets about how to use Azure server-less product Blob Storage and App Service to enable horizontally scalable web application for users to upload files (BLOBs).

open_in_new View open_in_new Azure

local_offer asp.net core local_offer gulp

visibility 270
thumb_up 0
access_time 2 years ago

Background If you have been working on ASP.NET projects in the past years, you probably have heard or used quite a few client library management frameworks/tools. For example, Bower, npm, Gulp, Grunt, Webpack, Yarn, Parcel, Libman, etc. Before SPA became popular, the default ASP.NET (or A...

open_in_new View open_in_new ASP.NET Core

local_offer asp.net core 2 local_offer asp.net core local_offer dotnetcore local_offer open-banking

visibility 231
thumb_up 0
access_time 2 years ago

I’ve just started an asp.net core 2.2 based implementation for Australia Consumer Data Standards (published by Data 61). Opening Banking initiative will follow these standards. The purpose is to help you to get familiar with these standards, especially the APIs that need to be implemented. ...

open_in_new View open_in_new ASP.NET Core

local_offer dotnet core local_offer asp.net core

visibility 4688
thumb_up 0
access_time 3 years ago

IP Address In ASP.NET Core, Request.UserHostAddress has been removed though that attribute exists in the traditional ASP.NET applications. We can use HttpContext.Connection to retrieve the remove client IP address: var ipAddress = HttpContext.Connecti...

open_in_new View open_in_new ASP.NET Core

info About author

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward

Kontext Column

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


Learn more arrow_forward