When debugging a Razor Server app locally using IISExpress I'm running my application as a test user by starting Chrome (or Edge) using Run As.
How can I get the username of the user that the browser is running as?
public CurrentUser(AuthenticationStateProvider authenticationStateProvider, IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
_authenticationStateProvider = authenticationStateProvider;
}
private readonly AuthenticationStateProvider _authenticationStateProvider;
private readonly IWebHostEnvironment _webHostEnvironment;
public string UserName
{
get
{
if (_webHostEnvironment.IsDevelopment())
{
// return System.Security.Principal.WindowsIdentity.GetCurrent().Name;
// return Environment.UserName;
return "All return Operating System username";
}
else
{
// returns null when debugging in IIS Express
var authState = Task.Run(_authenticationStateProvider.GetAuthenticationStateAsync).Result;
var user = authState.User;
var name = user.Identity.Name;
return name;
}
}
}