I'm building a Blazor server application for an internal company use and using Windows authentication. The app is supposed to field updates from another app via an exposed controller. I am trying to test the controller and getting a 401 unauthorized however I try to hit it (Postman, browser, test Console app, from within the Blazor server app itself, etc.). I'm running .NET 6 and scaffolded the app using Visual Studio and selecting Windows Authentication from the Create Project wizard for Blazor server app. I'm running the app from Visual Studio using IIS Express option. Here is the simple test I set up in the Console app just trying to get a positive response from the controller:
public async Task<int> GetUpdateTestTimerResult()
{
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:40871");
var url = "api/updateTest";
var value = await JsonSerializer.DeserializeAsync<int>
(await client.GetStreamAsync(url), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
return value;
}
I've tried setting the AllowAnonymous attribute on the controller but no dice there. How do I expose a controller that will allow an outside application (also internal to the company/on premises) access?
Edit: Per Jason (see below) I have tried adding UseDefaultCredentials to my request but it hasn't helped (I tried to upload an image but my company is apparently blocking that).
So it turns out I was just missing a line in my Program.cs. The Blazor project template doesn't automatically set up everything you need to expose controllers from your site like the WebAPI template does. You need to make sure you add builder.Services.AddControllers() and app.MapControllers() manually to your Program.cs. In my case I actually had added builder.Services.AddControllers() but I had neglected to add app.MapControllers(). It all works now.