I followed the documentation here and was able to get my controllers working with the [Authorize] header. I am using IdentityServer with ASP.NET Identity as my user store.
In my ConfigureServices I have:
services.AddIdentity<KipUser, IdentityRole>()
.AddEntityFrameworkStores<KipDbContext>()
.AddDefaultTokenProviders();
In my Configure I have:
app.UseIdentityServer();
app.UseAuthorization();
And in my controller I have done a few tests:
[Authorize]
public IEnumerable<MyDTO> GetData(int count = 3) {
var test = User; // The User.Identity.Name is empty for some reason
var id = User.FindFirst(ClaimTypes.NameIdentifier).Value; // Successfully gets the Guid
var test2 = _userManager.GetUserAsync(User); // result is null
var test3 = _userManager.GetUserId(User); //returns null
var test4 = _userManager.FindByIdAsync(id); // Successfully gets the User from the DB
It seems there is a misconfiguration because the UserManager fails to get the ASP.Net Identity from the ClaimsPrincipal User variable. Am I assuming too much that the .AddEntityFrameworkStores<KipDbContext>() would configure the application to know how to _userManager.GetUserAsync(User) instead of me needing to search for the claim myself with this _userManager.FindByIdAsync(User.FindFirst(ClaimTypes.NameIdentifier).Value) ?