Proper way to get current User ID in Entity Framework Core

后端 未结 4 857
灰色年华
灰色年华 2021-02-07 01:56

There are a bunch of different answers floating around here for the different RC\'s of ASP.NET Core on how to get the ID of the currently logged in user. I wanted to ask the def

4条回答
  •  面向向阳花
    2021-02-07 02:45

    ASP.NET Core Identity is injected via DI in the startup.cs - as such you just have to inject UserManager via a constructor

    UserManager userManager
    

    You can then use the following in methods

    _userManager.GetUserId(User);
    

    That's the way its used in the Sample Web Application when you create a new ASP.NET Core 1 project with Individual User Account.

提交回复
热议问题