services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
services.AddAuthorization(options => { options.AddPolicy("RequireLoggedIn", policy => policy.RequireAuthenticatedUser()); });
using Microsoft.AspNetCore.Http;
public class UserService { private readonly IHttpContextAccessor _httpContextAccessor;
public UserService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public string GetUserName()
{
return _httpContextAccessor.HttpContext.User.Identity.Name;
}
}
services.AddSingleton
@inject UserService UserService
Welcome, @UserService.GetUserName()!