{"id":30,"date":"2024-08-24T11:41:02","date_gmt":"2024-08-24T10:41:02","guid":{"rendered":"https:\/\/contentlabstudy.com\/soft\/?p=30"},"modified":"2024-08-24T11:41:02","modified_gmt":"2024-08-24T10:41:02","slug":"rest-minimal-api-with-authentication","status":"publish","type":"post","link":"https:\/\/contentlabstudy.com\/soft\/rest-minimal-api-with-authentication\/","title":{"rendered":"REST Minimal Api with Authentication"},"content":{"rendered":"\n<p>Adding external authentication providers like Azure AD or Google to a .NET Core Minimal API involves integrating the appropriate authentication libraries and configuring the services to use these providers. Below is a modified version of the original code that adds external authentication using Azure AD and Google.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Install Required NuGet Packages<\/h3>\n\n\n\n<p>First, you need to install the necessary NuGet packages to support authentication:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bashCopy code<code>dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer\ndotnet add package Microsoft.Identity.Web\ndotnet add package Microsoft.Identity.Web.UI\ndotnet add package Microsoft.AspNetCore.Authentication.Google\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Configure Authentication in <code>Program.cs<\/code><\/h3>\n\n\n\n<p>Modify the <code>Program.cs<\/code> file to configure authentication using Azure AD and Google:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">csharpCopy code<code>using Microsoft.AspNetCore.Authentication.JwtBearer;\nusing Microsoft.Identity.Web;\nusing Microsoft.AspNetCore.Authentication.Cookies;\nusing Microsoft.AspNetCore.Authentication.Google;\n\nvar builder = WebApplication.CreateBuilder(args);\n\n\/\/ Configure Azure AD Authentication\nbuilder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)\n    .AddMicrosoftIdentityWebApi(options =&gt;\n    {\n        builder.Configuration.Bind(\"AzureAd\", options);\n    });\n\n\/\/ Configure Google Authentication\nbuilder.Services.AddAuthentication(options =&gt;\n{\n    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;\n    options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;\n})\n.AddCookie()\n.AddGoogle(GoogleDefaults.AuthenticationScheme, options =&gt;\n{\n    options.ClientId = builder.Configuration[\"Authentication:Google:ClientId\"];\n    options.ClientSecret = builder.Configuration[\"Authentication:Google:ClientSecret\"];\n});\n\nbuilder.Services.AddAuthorization();\n\nvar app = builder.Build();\n\n\/\/ Enable authentication middleware\napp.UseAuthentication();\napp.UseAuthorization();\n\nvar products = new List&lt;Product&gt;\n{\n    new Product { Id = 1, Name = \"Product 1\", Price = 10.00m },\n    new Product { Id = 2, Name = \"Product 2\", Price = 20.00m },\n    new Product { Id = 3, Name = \"Product 3\", Price = 30.00m }\n};\n\n\/\/ Secure the endpoints with [Authorize] attribute\n\napp.MapGet(\"\/products\", () =&gt;\n{\n    return Results.Ok(products);\n}).RequireAuthorization();\n\napp.MapGet(\"\/products\/{id:int}\", (int id) =&gt;\n{\n    var product = products.FirstOrDefault(p =&gt; p.Id == id);\n    if (product is null)\n    {\n        return Results.NotFound();\n    }\n    return Results.Ok(product);\n}).RequireAuthorization();\n\napp.MapPost(\"\/products\", (Product product) =&gt;\n{\n    product.Id = products.Max(p =&gt; p.Id) + 1;\n    products.Add(product);\n    return Results.Created($\"\/products\/{product.Id}\", product);\n}).RequireAuthorization();\n\napp.MapPut(\"\/products\/{id:int}\", (int id, Product updatedProduct) =&gt;\n{\n    var product = products.FirstOrDefault(p =&gt; p.Id == id);\n    if (product is null)\n    {\n        return Results.NotFound();\n    }\n\n    product.Name = updatedProduct.Name;\n    product.Price = updatedProduct.Price;\n\n    return Results.Ok(product);\n}).RequireAuthorization();\n\napp.MapDelete(\"\/products\/{id:int}\", (int id) =&gt;\n{\n    var product = products.FirstOrDefault(p =&gt; p.Id == id);\n    if (product is null)\n    {\n        return Results.NotFound();\n    }\n\n    products.Remove(product);\n    return Results.NoContent();\n}).RequireAuthorization();\n\napp.Run();\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Configure Azure AD and Google in <code>appsettings.json<\/code><\/h3>\n\n\n\n<p>Add the configuration settings for Azure AD and Google to your <code>appsettings.json<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">jsonCopy code<code>{\n  \"AzureAd\": {\n    \"Instance\": \"https:\/\/login.microsoftonline.com\/\",\n    \"Domain\": \"yourtenant.onmicrosoft.com\",\n    \"TenantId\": \"your-tenant-id\",\n    \"ClientId\": \"your-client-id\",\n    \"ClientSecret\": \"your-client-secret\"\n  },\n  \"Authentication\": {\n    \"Google\": {\n      \"ClientId\": \"your-google-client-id\",\n      \"ClientSecret\": \"your-google-client-secret\"\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Secure the Endpoints<\/h3>\n\n\n\n<p>Notice that in the code, the <code>.RequireAuthorization()<\/code> method is used to secure each endpoint. This ensures that only authenticated users can access these endpoints.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Test the Application<\/h3>\n\n\n\n<p>Run the application and try accessing the secured endpoints. When you navigate to one of the secured routes, you should be redirected to the Azure AD or Google login page, depending on the configured provider.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Summary:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Authentication Setup:<\/strong> Configured external authentication using Azure AD and Google.<\/li>\n\n\n\n<li><strong>Endpoint Security:<\/strong> Used <code>.RequireAuthorization()<\/code> to secure API endpoints.<\/li>\n\n\n\n<li><strong>Configuration:<\/strong> Managed authentication settings through <code>appsettings.json<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>This setup allows your .NET Core Minimal API to authenticate users via external providers like Azure AD and Google, adding a robust security layer to your RESTful endpoints.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">References<\/h3>\n\n\n\n<p>Here are some useful web references that can help you increase your knowledge of implementing REST endpoints using .NET Core Minimal API with external authentication providers like Azure AD or Google:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Microsoft Learn &#8211; Minimal APIs in .NET<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> This documentation provides an in-depth introduction to Minimal APIs in .NET, covering how to create RESTful endpoints and integrate with authentication providers.<\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/core\/fundamentals\/minimal-apis?view=aspnetcore-6.0\">Microsoft Learn &#8211; Minimal APIs in .NET<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Microsoft Learn &#8211; Azure AD Authentication in ASP.NET Core<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> Detailed guide on integrating Azure Active Directory authentication with ASP.NET Core, including how to set up and configure Azure AD for your application.<\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/active-directory\/develop\/quickstart-v2-aspnet-core-webapp\">Microsoft Learn &#8211; Azure AD Authentication in ASP.NET Core<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Microsoft Identity Web Documentation<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> Documentation for the Microsoft.Identity.Web library, which simplifies integrating Azure AD authentication into .NET Core applications, including Minimal APIs.<\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/active-directory\/develop\/microsoft-identity-web\">Microsoft Identity Web Documentation<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>Microsoft Learn &#8211; Google Authentication in ASP.NET Core<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> A tutorial on how to set up Google authentication in ASP.NET Core applications, including configuration and implementation details.<\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/core\/security\/authentication\/social\/google-logins?view=aspnetcore-6.0\">Microsoft Learn &#8211; Google Authentication in ASP.NET Core<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>Code Maze &#8211; Authentication in ASP.NET Core with Google<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> A step-by-step guide on implementing Google authentication in ASP.NET Core, with practical examples that can be adapted for Minimal APIs.<\/li>\n\n\n\n<li><a>Code Maze &#8211; Authentication with Google<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>YouTube &#8211; ASP.NET Core Minimal APIs with Authentication by Tim Corey<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> A video tutorial that walks through setting up Minimal APIs in ASP.NET Core with authentication, covering both Azure AD and Google as providers.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=hQAu0YBVU7I\">YouTube &#8211; ASP.NET Core Minimal APIs with Authentication<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>GitHub &#8211; ASP.NET Core Minimal API Examples with Authentication<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> A GitHub repository with examples of Minimal APIs in .NET Core that include integration with external authentication providers like Azure AD and Google.<\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/dotnet\/aspnetcore\/tree\/main\/src\/Mvc\/Mvc.ApiExplorer\/samples\/MinimalApiSample\">GitHub &#8211; ASP.NET Core Minimal API Examples<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>Pluralsight &#8211; Authentication and Authorization in ASP.NET Core<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> An online course that covers various authentication and authorization mechanisms in ASP.NET Core, including the use of external providers like Azure AD and Google.<\/li>\n\n\n\n<li><a>Pluralsight &#8211; Authentication and Authorization in ASP.NET Core<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong>Azure Active Directory Developer Documentation<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> Comprehensive documentation for developers integrating Azure AD into their applications, including code samples and best practices.<\/li>\n\n\n\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/develop\/\">Azure Active Directory Developer Documentation<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">10. <strong>Google Identity Platform Documentation<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> Official documentation for Google Identity services, including OAuth 2.0 and OpenID Connect, and how to integrate them with your .NET Core applications.<\/li>\n\n\n\n<li><a>Google Identity Platform Documentation<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">11. <strong>Microsoft Identity and Authentication Library (MSAL)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> Documentation and tutorials for using MSAL (Microsoft Authentication Library) to authenticate users with Azure AD in .NET Core applications.<\/li>\n\n\n\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/develop\/msal-overview\">MSAL Documentation<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">12. <strong>TutorialsPoint &#8211; ASP.NET Core Minimal APIs<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> A tutorial that introduces Minimal APIs in ASP.NET Core and discusses integrating them with authentication and other middleware.<\/li>\n\n\n\n<li><a>TutorialsPoint &#8211; ASP.NET Core Minimal APIs<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">13. <strong>Stack Overflow &#8211; ASP.NET Core Minimal APIs with External Authentication<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> Community-driven Q&amp;A on implementing Minimal APIs in ASP.NET Core with external authentication providers like Azure AD and Google.<\/li>\n\n\n\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/69934421\/how-to-secure-asp-net-core-6-minimal-api-with-jwt-authentication\">Stack Overflow &#8211; ASP.NET Core Minimal APIs with Authentication<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">14. <strong>Reddit &#8211; .NET Core Minimal APIs and Authentication Discussion<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> A Reddit thread discussing best practices for using Minimal APIs with authentication in .NET Core, with insights and advice from experienced developers.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.reddit.com\/r\/dotnetcore\/\">Reddit &#8211; .NET Core Minimal APIs and Authentication<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">15. <strong>Dev.to &#8211; ASP.NET Core Minimal API Authentication<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overview:<\/strong> A blog post on Dev.to that covers setting up authentication in Minimal APIs using various external providers.<\/li>\n\n\n\n<li><a>Dev.to &#8211; ASP.NET Core Minimal API Authentication<\/a><\/li>\n<\/ul>\n\n\n\n<p>These resources provide a comprehensive overview of how to implement REST endpoints using .NET Core Minimal API with external authentication providers like Azure AD and Google, helping you to enhance the security and functionality of your web applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adding external authentication providers like Azure AD or Google to a .NET Core Minimal API involves integrating the appropriate authentication libraries and configuring the services to use these providers. Below is a modified version of the original code that adds external authentication using Azure AD and Google. Step 1: Install Required NuGet Packages First, you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-software-development"],"_links":{"self":[{"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/comments?post=30"}],"version-history":[{"count":1,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":31,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts\/30\/revisions\/31"}],"wp:attachment":[{"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}