Get current (logged) user in ASP.NET Core 3.0 .razor page

后端 未结 2 993
后悔当初
后悔当初 2021-01-06 09:44

I\'m testing the waters with a blazer server-side app and trying to get the logged user in a .razor page. This

UserManager.GetUserAsync(User)
2条回答
  •  难免孤独
    2021-01-06 10:16

    What I did:

    1. added this to Startup.ConfigureServices
    services.AddHttpContextAccessor();
    
    1. used this to get the username in my .razor page, these 2 lines first
    @inject UserManager UserManager
    @inject IHttpContextAccessor HttpContextAccessor
    
    1. then the call to show the username like this:

    Hello @UserManager.GetUserName(HttpContextAccessor.HttpContext.User)

提交回复
热议问题