'HttpContext.User in MVC action is not authenticated, but in Blazor he is
I am developing a Blazor WASM Application and want to export data as a CSV file. The problem is that in the MVC controller action, the user is not authenticated.
Component.razor
<button @onclick="DownloadCsvData">Download CSV</button>
@code {
void DownloadCsvData()
{
NavigationManager.NavigateTo("Export/AsCSV");
}
}
ExportController.cs
public ExportController : Controller
{
public FileStreamResult AsCSV()
{
// Why is Request.HttpContext.User.Identity empty
// at this point and IsAuthenticated is false?
// I need to access user claims here.
return GenerateCsv();
}
}
Startup.cs
services.AddHttpContextAccessor();
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAd"));
services.AddControllersWithViews();
app.UseAuthentication();
app.UseAuthorization();
Everywhere else in this application, the user is authenticated as expected after a successfull login. Did I forget something? Do I explicitly do something for enabling the authentication in the controller action?
Just tell me if you need more information or code snippets.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
