Why am I not getting my username in the windows authenticated Web Core API?

Viewed 113

This code in my Web Core API confirms that I am an authenticated user, but I am not getting my username, instead I am getting the name of the application pool. How do I fix this?

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 logfile 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 OK, the LoggedInUser is correct. But when I call from my code I get the incorrect loggedInUser shown above. The code I use to call the web api from my client app 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 have set the application pool type to all of the various options; applicationpoolidentity, networkservice, localservice, localsystem and tested the application each time. What on earth am I missing?

1 Answers
Related