How to use Api_Key instead of JWT
See original GitHub issueI’m trying to use Api_Key which is specified in header. My swagger settings are great i followed the documentation of FastEndpoints. What confuses me is the authentication part. It keeps saying I’m not authorized. I just want the user to use the API key to authenticate there is no login endpoint in my security.
My code:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddFastEndpoints();
builder.Services
.AddSwaggerDoc(s =>
{
s.DocumentName = "Initial Release";
s.Title = "My API";
s.Version = "v1.0";
s.AddAuth("ApiKey", new()
{
Type = OpenApiSecuritySchemeType.ApiKey,
Name = "api_key",
In = OpenApiSecurityApiKeyLocation.Header,
Description = "Fill in your Api Key"
});
}, addJWTBearerAuth: false);
var app = builder.Build();
app.UseAuthorization();
app.UseFastEndpoints();
app.UseOpenApi();
app.UseSwaggerUi3(c => c.ConfigureDefaults());
app.Run("http://localhost:3000");
Exception i receive:
System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Authentication.IAuthenticationService' has been registered.
Even when i add app.UseAuthentication(); its not working.
What am i doing wrong?
Issue Analytics
- State:
- Created a year ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
API keys vs JWT authorization: Which is best?
Typically, the API key provides only application-level security, giving every user the same access; whereas the JWT token provides user-level ...
Read more >API key vs JWT - which authentication to use and when
Both API key and JWT can provide authentication and authorization. API key is on project scope and JWT is on user scope. API...
Read more >Using JSON Web Tokens as API Keys
Most APIs today use an API Key to authenticate legitimate clients. ... You can return a stateless JWT instead, with the allowed scopes...
Read more >JWT vs API Key Auth for Machine to Machine APIs
The main difference between API Key auth and JWT token auth is that the JWT Token is self-contained - the information asserted by...
Read more >API Keys vs OAuth Tokens vs JSON Web Tokens
Whereas API keys and OAuth tokens are always used to access APIs, JSON Web Tokens (JWT) can be used in many different scenarios....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Swagger is anonymously accessible with the provided demo code.
updated the code above. works now. basically you need a custom policy to turn off authorization. earlier it seems like i forgot to reload/refresh swagger ui when testing. my bad 😉