diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/HomeController.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/HomeController.cs index ac4d810..6c830fc 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/HomeController.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Controllers/HomeController.cs @@ -1,9 +1,11 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using TodoApp.Portal.Models; namespace TodoApp.Portal.Controllers { + [Authorize] public class HomeController : Controller { private readonly ILogger _logger; diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Models/AppSettings.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Models/AppSettings.cs new file mode 100644 index 0000000..7200f80 --- /dev/null +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Models/AppSettings.cs @@ -0,0 +1,7 @@ +namespace TodoApp.Portal.Models +{ + public static class AppSettings + { + public static readonly string Identity = "Identity"; + } +} diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs index 0727468..4058eda 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Program.cs @@ -1,7 +1,21 @@ +using Microsoft.AspNetCore.Authentication.OpenIdConnect; +using Microsoft.Identity.Web; +using Microsoft.Identity.Web.UI; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; +using TodoApp.Portal.Models; + var builder = WebApplication.CreateBuilder(args); +builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration, AppSettings.Identity); +builder.Services.Configure(OpenIdConnectDefaults.AuthenticationScheme, options => +{ + options.ResponseType = OpenIdConnectResponseType.Code; + options.Scope.Add(options.ClientId); +}); + // Add services to the container. builder.Services.AddControllersWithViews(); +builder.Services.AddRazorPages().AddMicrosoftIdentityUI(); var app = builder.Build(); @@ -17,11 +31,15 @@ app.UseStaticFiles(); app.UseRouting(); - +app.UseAuthentication(); app.UseAuthorization(); -app.MapControllerRoute( - name: "default", - pattern: "{controller=Home}/{action=Index}/{id?}"); +app.UseEndpoints(endpoints => +{ + endpoints.MapControllerRoute( + name: "default", + pattern: "{controller=Home}/{action=Index}/{id?}"); + endpoints.MapRazorPages(); +}); app.Run(); diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/TodoApp.Portal.csproj b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/TodoApp.Portal.csproj index c78c9c7..be55ce5 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/TodoApp.Portal.csproj +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/TodoApp.Portal.csproj @@ -6,4 +6,9 @@ enable + + + + + diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Shared/_Layout.cshtml b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Shared/_Layout.cshtml index 7081751..6e456fe 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Shared/_Layout.cshtml +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/Views/Shared/_Layout.cshtml @@ -1,4 +1,5 @@ - +@using System.Security.Principal + @@ -25,6 +26,12 @@ + @if (User.Identity?.IsAuthenticated ?? false) + { + + } diff --git a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/appsettings.json b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/appsettings.json index 10f68b8..020b47d 100644 --- a/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/appsettings.json +++ b/azure-app-service-vnet-template/Source/TodoApp/TodoApp.Portal/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, + "Identity": { + "Instance": "https://todoappb2c01.b2clogin.com", + "Domain": "todoappb2c01.onmicrosoft.com", + "ClientId": "7549b5d6-b011-4140-a7f9-5690cbc79157", + "SignUpSignInPolicyId": "B2C_1_signupsignin", + "ResponseType": "code" + }, "AllowedHosts": "*" }