Тёмный

How To Implement API Key Authentication In ASP.NET Core 

Milan Jovanović
Подписаться 94 тыс.
Просмотров 33 тыс.
50% 1

☄️ Master the Modular Monolith Architecture: bit.ly/3SXlzSt
📌 Accelerate your Clean Architecture skills: bit.ly/3PupkOJ
🚀 Support me on Patreon to access the source code: / milanjovanovic
API Key Authentication is commonly used in S2S scenarios. You provide the API caller a unique key that they use to authenticate with your API. The API Key can be sent in a few ways. For example, it can be sent in a request header, the query string, or in a cookie. I'll show you how to implement API Key authentication in Minimal APIs and how to do it with controllers.
Join my weekly .NET newsletter:
www.milanjovanovic.tech
How To Implement API Key Authentication In ASP.NET Core
www.milanjovanovic.tech/blog/...
Read my Blog here:
www.milanjovanovic.tech/blog
Subscribe for more:
/ @milanjovanovictech
Chapters
0:00 Defining an endpoint filter
1:50 Implementing ApiKeyAuthenticationEndpointFilter
6:45 How an endpoint filter works
8:37 Validating the API key
12:32 Alternatives to sending the API key in the header
13:22 Alternatives to storing API keys in the application configuration
14:12 API key authentication with controllers

Наука

Опубликовано:

 

1 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 64   
@MilanJovanovicTech
@MilanJovanovicTech Год назад
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
@BK-19
@BK-19 Год назад
Nice, easy clean explaination, will definetly use in my next minimal API project.
@MilanJovanovicTech
@MilanJovanovicTech Год назад
Glad it was helpful!
@JosephRuhundwa
@JosephRuhundwa Год назад
Thank you @Milan for your great content
@MilanJovanovicTech
@MilanJovanovicTech Год назад
You're very welcome!
@peymannaji
@peymannaji 10 месяцев назад
It was very useful Milan, Thanks a lot!
@MilanJovanovicTech
@MilanJovanovicTech 10 месяцев назад
Glad it was helpful!
@amrnouh3414
@amrnouh3414 Год назад
Thanks for your awesome content and simplified explaination keep it up ❤️👏
@MilanJovanovicTech
@MilanJovanovicTech Год назад
My pleasure 😊
@RioTheHitman
@RioTheHitman Год назад
This is absolutely beautiful thank you
@MilanJovanovicTech
@MilanJovanovicTech Год назад
You are so welcome
@Tamer_Ali
@Tamer_Ali Год назад
Thanks Milan, you awesome 👍
@MilanJovanovicTech
@MilanJovanovicTech Год назад
Thanks a lot for watching 😁
@DigiWorldHub
@DigiWorldHub 7 месяцев назад
Looks good for the beginner. Thanks.
@MilanJovanovicTech
@MilanJovanovicTech 7 месяцев назад
No problem!
@ArgFernando
@ArgFernando Год назад
👍👍 Thx for share !!
@MilanJovanovicTech
@MilanJovanovicTech Год назад
You're welcome :)
@masterofclay123
@masterofclay123 9 месяцев назад
Nice vid. Pretty cool.
@MilanJovanovicTech
@MilanJovanovicTech 9 месяцев назад
Glad you enjoyed it
@nouchance
@nouchance Год назад
THANK YOU SIR
@MilanJovanovicTech
@MilanJovanovicTech Год назад
Most welcome
@10Totti
@10Totti Год назад
Best!
@MilanJovanovicTech
@MilanJovanovicTech Год назад
Thanks!
@Tamer_Ali
@Tamer_Ali Год назад
Hi Milan, I hope you show us how to use commands and queries to write and read from database using different contexts for better performance for enterprise projects like e-commerce for example Command write to SQL database Queries read using Elastic Search
@MilanJovanovicTech
@MilanJovanovicTech Год назад
That would be such an interesting video 😁😁 Definitely planning more stuff around distributed systems.
@onedev7316
@onedev7316 Год назад
Very nice explanation as always. Is there any chance you can do a video on user impersonation using JWT/Identity please.
@MilanJovanovicTech
@MilanJovanovicTech Год назад
I don't find that feature compelling enough to dedicate a video to it, for now
@Ahmed-ui5wn
@Ahmed-ui5wn 6 месяцев назад
Great explanation as usual. Would you please share with us the theme you are using in Visual Studio. Thanks. Keep it up.
@MilanJovanovicTech
@MilanJovanovicTech 6 месяцев назад
ReSharper
@reggyA898
@reggyA898 Год назад
Hey, one question, what are u doing in case if mulptiple commands using the same logic partually, it can contains request to database, or just logic without requests to database. Where do u store common code like that? Or u just repeat same code in both commands because the idea of commands that they are independent
@MilanJovanovicTech
@MilanJovanovicTech Год назад
I would either: - repeat same code in both commands if it is 2 situations - store common code in a service if it's more than that
@reggyA898
@reggyA898 Год назад
@@MilanJovanovicTech thanks!
@mylesdavies9476
@mylesdavies9476 Год назад
What are your thoughts on using middleware to check for the key, this is something I have done previously
@MilanJovanovicTech
@MilanJovanovicTech Год назад
Works just fine 😁
@microtech2448
@microtech2448 Год назад
Can you please explain how this can be added as custom authentication handler which can be added in startup woth its own custom scheme name in addition to inbuilt jwtbeater or cookie authenticators so that an api endpoint can be authenticated either through jwt token or api key? Thank you!
@MilanJovanovicTech
@MilanJovanovicTech Год назад
Hmm, could make for an interesting video
@barr5221
@barr5221 Год назад
Milan what about an episode where you explain message metadata? For example: -Authentication token / API key -Correlation Id -Causation id -UTC Timestamp -Culture / Localization of the client -Message version -Message unique id -others? What do you think is necessary?
@barr5221
@barr5221 Год назад
At least Correlation Id is always MUST BE for my APIs. How can you solve issues on production without it? At my current job we use Authorization token and Culture as well. When some problem arises I really would like to have all of them.
@MilanJovanovicTech
@MilanJovanovicTech Год назад
I like the idea, I'll see what I can come up with!
@ArnonDanon
@ArnonDanon Год назад
Hi Milan, how do you secure this key with your users when you hand them the key? Do you recomend some key rotation mechanism? How would you handle revocation of a stolen key and hand a new key to the customer? I hope to hear your approch for those scenarios.
@MilanJovanovicTech
@MilanJovanovicTech Год назад
You can't really secure it once you hand it over to the users. Same with user passwords. If they're compromised, there is nothing you can do about it. With API keys, revocation is easy. Create a new key, hand it to the customer, and give them a grace period to switch to the new key.
@ArnonDanon
@ArnonDanon Год назад
@@MilanJovanovicTech thanks for taking the time to answer it
@VeggehGaming
@VeggehGaming Год назад
Would it be worth it to have the API key in the header hashed and then check the hash with a hashed version of any valid API keys you have in order to increase security?
@MilanJovanovicTech
@MilanJovanovicTech Год назад
Probably, it makes a lot of sense
@ferenc3460
@ferenc3460 Год назад
Hi! Can you show us an implementation about how to use this or JWT in a MAUI or WPF application (even with refresh token)?
@MilanJovanovicTech
@MilanJovanovicTech Год назад
Sounds fun, I'll add it to my content list
@ferenc3460
@ferenc3460 Год назад
@@MilanJovanovicTech thanks! I have no problem to implement JWT tokens into a web app and save it in the cookie but I can't really find too much information and clean solution how to do it in desktop and mobile application.
@fillegar
@fillegar Год назад
Your tweet said the video would cover: - Great at limiting access to an API - Tracking usage and billing I don’t see those topics listed in the chapters. Which chapters cover them? Or is there another video? Thanks in advance.
@MilanJovanovicTech
@MilanJovanovicTech Год назад
Oh, sorry if it was a little misleading. I kind of shared in the Tweet "what I learned", while the video is more focused on "how to implement authentication" only
@MilanJovanovicTech
@MilanJovanovicTech Год назад
I'll see to tackle tracking/billing in a future video, it's a nice topic
@jeffersantosss
@jeffersantosss Год назад
Milan, how to implement support for multiple authentication methods in Swagger, including Bearer Token and API Key?
@MilanJovanovicTech
@MilanJovanovicTech Год назад
I'm not sure honestly, Swagger is a bit tricky
@roberteru25
@roberteru25 Год назад
// add JWT Authentication var securityScheme = new OpenApiSecurityScheme { Name = "JWT Authentication", Description = "Enter JWT Bearer token **Bearer token**", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, Scheme = "bearer", // must be lower case BearerFormat = "JWT", Reference = new OpenApiReference { Id = JwtBearerDefaults.AuthenticationScheme, Type = ReferenceType.SecurityScheme } }; x.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme); x.AddSecurityRequirement(new OpenApiSecurityRequirement { {securityScheme, Array.Empty()} }); // add Basic Authentication var basicSecurityScheme = new OpenApiSecurityScheme { Type = SecuritySchemeType.ApiKey, In = ParameterLocation.Header, Name = "X-API-KEY", Reference = new OpenApiReference { Id = "ApiKey", Type = ReferenceType.SecurityScheme } }; x.AddSecurityDefinition(basicSecurityScheme.Reference.Id, basicSecurityScheme); x.AddSecurityRequirement(new OpenApiSecurityRequirement { {basicSecurityScheme, Array.Empty()} });
@danyaracena222
@danyaracena222 Год назад
Can the Implement API Key Authentication be used for a multi-tenant structure?
@MilanJovanovicTech
@MilanJovanovicTech Год назад
Yes, you'll just need more API Keys. One for each tenant. And also, based on the API Key provided in the request, you'll have to extract the Tenant ID information.
@danyaracena222
@danyaracena222 Год назад
@@MilanJovanovicTech Thank you for video is good resource.
@techpc5453
@techpc5453 Год назад
from saudi arabia
@MilanJovanovicTech
@MilanJovanovicTech Год назад
👋 from Serbia
@techpc5453
@techpc5453 Год назад
@@MilanJovanovicTech best people from Serbia And Russia
@MarcusKaseder
@MarcusKaseder Год назад
Unfortunately you've mixed up Authentication and Authorization. If it is about Authentication like you've mentioned in your title and folder, you should've set a valid Principal like ClaimsPrincipal to the context - nothing more. If it is about Authorization like you've mentioned in your filter and the controller interface, the filter is correct if it returns a Unauthorized result 😉
@MilanJovanovicTech
@MilanJovanovicTech Год назад
I wouldn't agree - try to detach the naming conventions from what I'm actually doing. Authentication - who is calling the API. Authorization - what can the caller do (roles, permissions) It's an unfortunate circumstance that the HTTP Status code is 401 Unauthorized, which represents a not-authenticated user. And 403 Forbidden for a user without permissions. You'll notice that the name of the attribute in MVC is Authorize - which tackles Authentication - but is called so to match the respective status code.
@MarcusKaseder
@MarcusKaseder Год назад
@@MilanJovanovicTech Oh yeah, true! Absolutely forgot about the 403. It is rarely used in our applications because we don't want to expose api internals to the client. (security reasons)
Далее
Implementing API Key Authentication in ASP.NET Core
17:57
Штаны легионера
00:44
Просмотров 372 тыс.
🌊🌊🌊
01:01
Просмотров 809 тыс.
Как пронести Конфеты В ТЮРЬМУ
19:16
The RIGHT Way To Use HttpClient In .NET
11:46
Просмотров 55 тыс.
API Key Authentication Best Practices
25:56
Просмотров 27 тыс.
Background Tasks Are Finally Fixed in .NET 8
10:29
Просмотров 105 тыс.