asp.netasp.net-identityasp.net-core-mvcasp.net-core-1.0

How to get the current logged in user ID in ASP.NET Core?


I've done this before with MVC5 using User.Identity.GetUserId() but that doesn't seem to work here. The User.Identity doesn't have the GetUserId() method.

I am using Microsoft.AspNet.Identity.


Solution

  • I included using System.Security.Claims and I could access the GetUserId() extension method

    NB: I had the using Microsoft.AspNet.Identity already but couldn't get the extension method. So I guess both of them have to be used in conjunction with one another

    using Microsoft.AspNet.Identity;
    using System.Security.Claims;
    

    EDIT: This answer is now outdated. Look at Soren's or Adrien's answer for a dated way of achieving this in CORE 1.0