June 22, 2021 by Anuraj Estimated read time : 6 mins
This article will discuss about minimal APIs in ASP.NET Core 6.0 - How to implement Authentication and some C# 10 features which will help to minimize the code. Few days back I wrote a post of Minimal APIs in ASP.NET Core 6.0. I received one comment, asking about how to implement authentication in Minimal APIs. So I thought I will write another blog post on how to implement authentication. Like I responded to the comment authentication implementation is in similar way as we are implementing now.
Here is the code for implementing Token authentication in Minimal API.
Next you can implement the Token endpoint like this.
And finally map the endpoints
If you’re using Visual Studio 2022 Preview 1, you can use C# 10 features by modifying Project file. You can checkout this blog post on how to do it. Once you enable C# 10 features you can move all the using statements to a separate file explicit cast to (Func<string>) will no longer be necessary, and you can add attributes to lambda expressions and lambda parameters. So you don’t need to create separate methods. You can do implement the code in the Map delegate. Here is the C# 10 code.