I am trying to create an ASP.NET Core with React.js project with API authorization but struggling to find documentation/instructions that make sense.
https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-7.0 seems like a good reference, but it is using Entity Framework which I am not. My goal is to manage user authentication without EF.
The React template created by dotnet new react -au Individual provides AuthorizeService.js and OidcConfigurationController.cs which I have linked here: https://gist.github.com/julesx/d3daa6ed5a7f905c984a3fedf02004c0
My program.cs is as follows:
using Duende.IdentityServer.Models;
using Microsoft.AspNetCore.Authentication;
var ApiScopes = new List<ApiScope> {
new ApiScope("api1", "My API")
};
var Clients = new List<Client> {
new Client {
ClientId = "client",
// no interactive user, use the clientid/secret for authentication
AllowedGrantTypes = GrantTypes.ClientCredentials,
// secret for authentication
ClientSecrets =
{
new Secret("secret".Sha256())
},
// scopes that client has access to
AllowedScopes = { "api1" }
}
};
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiScopes(ApiScopes)
.AddInMemoryClients(Clients);
builder.Services.AddAuthentication()
.AddIdentityServerJwt();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseIdentityServer();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html");
app.Run();
After struggling to get this far, the app starts successfully (dev environment).
My fetch from the front end is as follows:
export async function getKpiData(): Promise<IRawKpi[]> {
const token = await authService.getAccessToken();
const response = await fetch('/kpidata', {
headers: !token ? {} : { 'Authorization': `Bearer ${token}` }
});
if (response.status == 200) {
return response.json();
}
return [];
}
this causes a get request to the OidcConfigurationController which fails with following error:
Unable to resolve service for type 'Microsoft.AspNetCore.ApiAuthorization.IdentityServer.IClientRequestParametersProvider' while attempting to activate 'MyNamespace.Controllers.OidcConfigurationController'.
I know this is occurring because I am not registering the IClientRequestParametersProvider injected into the OidcConfigurationController, however when I look at the sample code I don't see it being injected there either. I also do not see anything obvious I should be injecting into the Program.cs builder.Services.
Am I on the right track at all? The amount of "arcane" knowledge required to configure this seems overwhelming. Is there a quality example somewhere I can refer to? What is the bare minimum requirement for Program.cs to achieve some super basic authentication?