question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use Api_Key instead of JWT

See original GitHub issue

I’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:closed
  • Created a year ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
EthemAcar-Devcommented, Jun 13, 2023

There’s one thing I don’t understand, though. Since .UseSwaggerGen() is used after calling .UseAuthorization() it means the /swagger web page needs authentication too. So, the only way to access the docs is to manually edit the API key header. Can you make the swagger document accessible anonymously?

Swagger is anonymously accessible with the provided demo code.

2reactions
dj-nitehawkcommented, Apr 5, 2022

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 😉

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found