I created a Blazor Webassembly app from the included template in VS with Authorization and ASP.NET Core hosted options as shown here:
I want to be able to make http requests to the server without being authenticated. I changed the code in the WeatherForecastController
by commenting out the [Authorize]
attribute (and even added an [AllowAnonymous]
attribute):
//[Authorize] CHANGED
[AllowAnonymous] //CHANGED
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
In the FetchData.razor
page, along with other changes noted in the code, I commented out @attribute [Authorize]
:
@page "/fetchdata"
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using StackOverflowAuthProblem.Shared
@*@attribute [Authorize]*@ @*CHANGED*@
@inject HttpClient Http
@*CHANGED Html removed for brevity*@
<div>Exception message: @exceptionMessage</div>
@code {
private WeatherForecast[] forecasts;
string exceptionMessage; //CHANGED
protected override async Task OnInitializedAsync()
{
try
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
}
catch (AccessTokenNotAvailableException exception)
{
exceptionMessage = exception.Message; //CHANGED returns an empty string
exceptionMessage = exception.ToString(); //CHANGED returns
//Microsoft.AspNetCore.Components.WebAssembly.Authentication.AccessTokenNotAvailableException: '' at
//Microsoft.AspNetCore.Components.WebAssembly.Authentication.AuthorizationMessageHandler.SendAsync(HttpRequestMessage request,
//CancellationToken cancellationToken) at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(
// HttpRequestMessage request, CancellationToken cancellationToken)
//at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request,
//HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop,
//CancellationToken cancellationToken)
//at System.Net.Http.Json.HttpClientJsonExtensions.<GetFromJsonAsyncCore>d__9`1[[StackOverflowAuthProblem.Shared.WeatherForecast[],
//StackOverflowAuthProblem.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
//at StackOverflowAuthProblem.Client.Pages.FetchData.OnInitializedAsync()
//in E:StackOverflowStackOverflowAuthProblemStackOverflowAuthProblemClientPagesFetchData.razor:line 53
//exception.Redirect(); CHANGE
}
}
}
The exception I get is in the code above. I suspect the problem is in the App.razor
page, but can't figure it out.
Any help?
question from:
https://stackoverflow.com/questions/65661659/make-anonymous-call-to-server-in-asp-net-core-hosted-blazor-app 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…