Why am I not getting my username in an authenticated Windows kernel API?

This code in my web kernel API confirms that I am an authenticated user, but I do not get my username, instead I get the name of the application pool. How to fix it?

var testa = User.Identity.GetType();
NLogger.Info($"testa = {testa}");
var testd = User.Identity.IsAuthenticated;
NLogger.Info($"testd = {testd}");
var loggedInUser = User.Identity.Name;
NLogger.Info($"loggedInUser = {loggedInUser}");

In my log file, I get:

testa = System.Security.Principal.WindowsIdentity
testd = True
loggedInUser = IIS APPPOOL\SIR.WEBUI

I use the [Authorize] tag for the controller, and anonymous authentication is disabled.

Well, I call the method from Postman, it works fine, LoggedInUser is correct. But when I call from my code, I get the wrong loggedInUser shown above. The code I use to call the web api from my client application is:

public static async Task<IEnumerable<ContractDto>> GetAllForUser()
{
    using (var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }))
    {
        client.BaseAddress = new Uri(AppSettings.ServerPathApi);
        var path = GetPath("getallforuser");
        var response = await client.GetAsync(path);
        response.EnsureSuccessStatusCode();
        var stringResult = await response.Content.ReadAsStringAsync();
        return JsonConvert.DeserializeObject<IEnumerable<ContractDto>>(stringResult);
    }
}

In IIS, I set the application pool type for all of the various options; applicationpoolidentity, networkervice, localservice, localsystem and each time tested the application. What am I missing?

+6
1

if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
  string username = System.Web.HttpContext.Current.User.Identity.Name;
}

var user = (System.Security.Principal.WindowsIdentity)HttpContext.Current.User.Identity;
var userName = user.Impersonate();
0

All Articles