0

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).

2 Answers 2

0

After investigating the issue, using UseDefaultCredentials will help us fix the issue.

        HttpClientHandler handler = new HttpClientHandler()
        {
            UseDefaultCredentials = true
        };
        var client = new HttpClient(handler);
        client.BaseAddress = new Uri("http://localhost:8008/");
        var url = "error?handler=api";

        var response = await client.GetAsync(url);
        return new JsonResult(response);

Test Result

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply! Unfortunately that did not solve the issue for me though.
Hi @Dennis , could you create a brand new sample which can reproduce the issue for me? Please hide your sensitive information, and also share the detailed steps, many thanks
Hi @Jason. So I created two projects: a Blazor Server app and a Console app. For the Blazor server app, all I did was create the app targeting .NET 6.0 selecting Windows authentication in the project wizard. I then added an empty API controller with one simple HttpGet method that just returned IActionResult OK(teststring). That's all I did for the Blazor app. For the Console app, I created it against .NET 6.0 as well. I created a class that exposed the method shown in my original post, adding in the UseDefaultCredentials. Then I added code to Program to run that.
If you tell me how to get you the code, I'll send what I have. But I didn't do much. Just what I detailed above. I tried running in IIS Express mode and project mode from VS. Same result both ways.
Hi @Jason. I was actually working on this last night and I believe I figured out the problem. The VS Blazor project templates don't automatically set up everything you need to configure in Program.cs if you want to be able to create a controller to access. I had to manually add 'UseControllers' and 'MapControllers' lines to my Program.cs. This fixed the issue on my quick sample project. I'm going to try it on my actual project today or tomorrow and if that works I'll post it as an answer. Thanks!
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.