This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy. Allow Cookies and Dismiss

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

662 views 0 comments last modified about 9 months ago Raymond

asp.net core identity core 2

In this page

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.

Related pages

ASP.NET Core 2 with Bootstrap 4 Bundler Minifier Issue: Expected semicolon or closing curly-brace found '-'

561 views   1 comments last modified about 5 months ago

Background When upgrading Bootstrap to v4.0.0 release, the bundler and minifier doesn’t work properly due to CSS variable is commonly used: :root{--blue:#007bff; About 26 errors will show up in the Error List with the following message: ...

View detail

Issue - Unable to get property 'apply' of undefined or null reference occurred in Angular 4.*, VS2017 15.3, ASP.NET Core 2.0

6133 views   10 comments last modified about 2 years ago

Issue Context After installed Visual Studio 2017 15.3 preview and .net core 2.0 preview SDK, I upgraded one of my existing asp.net core project to 2.0. The project was created using ‘dotnet new angular’ SPA template.&nbsp; I also upgraded all the client app packages to the latest. For exa...

View detail

Retrieve Http client request metadata like IP address and languages in asp.net core

805 views   0 comments last modified about 9 months 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...

View detail

Retrieve Azure AppSettings and Connection String Settings in ASP.NET Core Apps

1150 views   0 comments last modified about 9 months ago

In ASP.NET Core, we can easily use user secrets to manage our password or credentials. This post will summarize the approaches we can use after the websites are deployed into Azure.

View detail

Retrieve Azure AppSettings and Connection String Settings in ASP.NET Core Apps

1150 views   0 comments last modified about 9 months ago

In ASP.NET Core, we can easily use user secrets to manage our password or credentials. This post will summarize the approaches we can use after the websites are deployed into Azure.

View detail

Server.MapPath Equivalent in ASP.NET Core 2

3472 views   0 comments last modified about 9 months ago

In traditional asp.net applications, Server.MapPath is commonly used to generate absolute path in the web server. However, this has been removed from ASP.NET Core. So what is the equivalent way of doing it?

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.