ASP.NET Web API get user identity in controller constructor

Alexey Z.

Is good idea to get user identity in ASP.NET Web API controller constructor, for example:

public PagesController(PageValidator pageValidator, PageMapper pageMapper, PagesManager pagesManager, UsersManager usersManager)
            :base(usersManager)
        {
          _pageValidator = pageValidator;
          _pageMapper = pageMapper;
          _pagesManager = pagesManager;
          if (User.Identity.IsAuthenticated)
            _pagesManager.UserId = usersManager.GetByEmail(User.Identity.Name).Id;
        }

Is always User.Identity was correct populated before this call raise?

Maksym Voronytskyi

Yes. You can use this property in Controller in any place. ASP.NET has request pipeline: (http://www.dotnetcurry.com/aspnet/888/aspnet-webapi-message-lifecycle). As you can see Authorization is early stage step in request pipeline. Controller creation is the latest stage.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get User Roles with ASP.net Identity and Web API

How to get logged-in user name in the constructor of a controller in ASP.NET Core Identity

Access user identity from controller in ASP.NET Core Web API

ASP.Net Identity with IdentityServer fails to get User in controller context

How to get JWT authenticated sign-in user from identity claims in asp.net web api

ASP.NET Web API controller constructor called with each request

Single controller with multiple GET methods in ASP.NET Web API

ASP.NET Core 3.1: User.Identity.Name is empty in API Controller, but claim name is present

User.Identity.Name is empty in Asp.NET Core 2.0 API Controller

ASP.NET Identity2 - How to get User Id with AllowAnonymous Controller?

ASP.NET Core Identity in a Web API

How to check if user is logged in to ASP.NET Core web application when using ASP.NET Core Web API to house identity

How get data from asp.net web api in asp.net mvc controller in the same project

Get user profile in ASP.net core API with Identity Server 4 Implicit Flow

ASP.Net MVC Controller Constructor shows error System.Web.Mvc.Controller.Session.get returned null

Ignore controller in ASP.NET Web API

how to get Identity PasswordOptions in ASP.NET MVC controller

How can I find out the name of the user when in a ASP.Net Web API Controller?

ASP.Net Core API: How to link User Identity to a Contact

User Authentication in ASP.NET Web API

ASP.NET Core Identity - get current user

Get current user id in ASP.NET Identity 2.0

Get ASP.NET Identity Current User In View

Get Authenticated Identity User data in ASP.NET MVC

How to get Asp.net Core Identity User in View

How to get role name for user in Asp.Net Identity

Simple Injector Registration ASP.NET identity & Web API

Asp.Net Identity connecting to DB via Web Api

Restrict access to register users in ASP.NET Web API and Identity