From 3041520419b984d3e69f3aadf5d5bc9ae8162a15 Mon Sep 17 00:00:00 2001 From: Roger Howell Date: Mon, 22 Jan 2024 23:56:46 +0000 Subject: [PATCH] Revert "Enable authentication to the service using active directory / entra / microsoft account" This reverts commit 80f0a72d561323269d98a84c54f953903d37e1cb. --- .../Models/GraphUserClient.cs | 54 ----------- .../Pages/Me/Index.cshtml | 94 ------------------- .../Pages/Me/Index.cshtml.cs | 31 ------ .../Pages/Shared/_DfE_1200px_Layout.cshtml | 42 ++++----- .../Pages/Shared/_LoginPartial.cshtml | 20 +++- .../Program.cs | 49 ++++------ .../ServiceAssessmentService.WebApp.csproj | 9 +- .../appsettings.Development.json | 9 -- .../appsettings.json | 13 --- 9 files changed, 54 insertions(+), 267 deletions(-) delete mode 100644 src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Models/GraphUserClient.cs delete mode 100644 src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Me/Index.cshtml delete mode 100644 src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Me/Index.cshtml.cs diff --git a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Models/GraphUserClient.cs b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Models/GraphUserClient.cs deleted file mode 100644 index 60135d64..00000000 --- a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Models/GraphUserClient.cs +++ /dev/null @@ -1,54 +0,0 @@ -using Microsoft.Graph; -using ServiceAssessmentService.WebApp.Pages.Me; - -namespace ServiceAssessmentService.WebApp.Models; - -public class GraphUserClient -{ - - private readonly GraphServiceClient _graphServiceClient; - private readonly ILogger _logger; - - private Microsoft.Graph.User? _graphUser = null; - - public GraphUserClient(GraphServiceClient graphServiceClient, ILogger logger) - { - _graphServiceClient = graphServiceClient; - _logger = logger; - } - - public async Task GetGraphUser() - { - if (_graphUser == null) - { - _graphUser = await _graphServiceClient.Me.Request().GetAsync(); - } - - return _graphUser; - } - - public async Task GetGraphUserDisplayName() - { - var graphUser = await GetGraphUser(); - return graphUser.DisplayName; - } - - public async Task GetGraphUserGivenName() - { - var graphUser = await GetGraphUser(); - return graphUser.GivenName; - } - - public async Task GetGraphUserSurname() - { - var graphUser = await GetGraphUser(); - return graphUser.Surname; - } - - public async Task GetGraphUserMail() - { - var graphUser = await GetGraphUser(); - return graphUser.Mail; - } - -} diff --git a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Me/Index.cshtml b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Me/Index.cshtml deleted file mode 100644 index f9be45ef..00000000 --- a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Me/Index.cshtml +++ /dev/null @@ -1,94 +0,0 @@ -@page -@model ServiceAssessmentService.WebApp.Pages.Me.IndexModel -@{ - ViewData["Title"] = "Home page"; - - var graphUser = ViewData["GraphUser"] as Microsoft.Graph.User; -} - -
- -
- -
-
-

Graph API result

- -
    -
  • @ViewData["GraphApiResult"]
  • -
  • @graphUser.GivenName
  • -
  • @graphUser.Surname
  • -
  • @graphUser.Mail
  • -
-
-
- -
-
-

User Properties

- -
- @foreach (var property in Model.User.GetType().GetProperties()) - { -
-
- @property.Name -
-
- @property.GetValue(Model.User) -
-
- } -
-
-
- -
-
-

Claims

- -
- @foreach (var claim in Model.User.Claims) - { -
-
- @claim.Type -
-
- @claim.Value -
-
- } -
-
-
- - -
-
-

User Properties

- -
- @foreach (var property in graphUser.GetType().GetProperties()) - { - var value = property.GetValue(graphUser); - if (value is null) - { - continue; - } - -
-
- @property.Name -
-
- @value -
-
- } -
-
-
- -
-
diff --git a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Me/Index.cshtml.cs b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Me/Index.cshtml.cs deleted file mode 100644 index c096f2e5..00000000 --- a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Me/Index.cshtml.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; -using Microsoft.Identity.Web; -using System.Net; -using Microsoft.AspNetCore.Authorization; -using Microsoft.Graph; -using ServiceAssessmentService.WebApp.Models; - -namespace ServiceAssessmentService.WebApp.Pages.Me; - -[AuthorizeForScopes(ScopeKeySection = "MicrosoftGraph:Scopes")] -public class IndexModel : PageModel -{ - private readonly GraphServiceClient _graphServiceClient; - private readonly GraphUserClient _graphUserClient; - private readonly ILogger _logger; - - public IndexModel(ILogger logger, GraphServiceClient graphServiceClient, GraphUserClient graphUserClient) - { - _logger = logger; - _graphServiceClient = graphServiceClient; - _graphUserClient = graphUserClient; - } - - public async Task OnGet() - { - var user = await _graphUserClient.GetGraphUser(); - ViewData["GraphUser"] = user; - ViewData["GraphApiResult"] = user.DisplayName; - } -} diff --git a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Shared/_DfE_1200px_Layout.cshtml b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Shared/_DfE_1200px_Layout.cshtml index 1d5e132e..0fcc542a 100644 --- a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Shared/_DfE_1200px_Layout.cshtml +++ b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Shared/_DfE_1200px_Layout.cshtml @@ -2,6 +2,8 @@ @using ServiceAssessmentService.Data.Entities @using GovUk.Frontend.AspNetCore.TagHelpers +@inject SignInManager SignInManager +@inject UserManager UserManager @{ // ReSharper disable once Razor.LayoutNotResolved @@ -10,10 +12,10 @@ // Use the page title, suffixed by the service name ViewData["Title"] += " - Service Assessment Service"; - + // get name of current area var area = ViewContext.RouteData.Values["area"] as string; - + // get name of current controller var controller = ViewContext.RouteData.Values["controller"] as string; @@ -38,14 +40,14 @@ // Helpers, used to determine which navigation item is currently active var isAreaDefault = (area is null) || ("Home".Equals(area, StringComparison.OrdinalIgnoreCase)); var isAreaBook = ("Book".Equals(area, StringComparison.OrdinalIgnoreCase)); - + } @* ReSharper disable once Razor.SectionNotResolved *@ @section Head { - + } @@ -71,7 +73,7 @@ @* Sign out *@ @* *@ @* *@ - +
@@ -91,27 +93,28 @@

    - @if (User.Identity.IsAuthenticated) + @if (SignInManager.IsSignedIn(User)) {
  • Dashboard
  • } - @if (User.Identity.IsAuthenticated) + @if (SignInManager.IsSignedIn(User)) { @*
  • *@ @* *@ @@ -124,26 +127,13 @@ @* *@ @*
  • *@ } - @if (User.Identity.IsAuthenticated) + @if (SignInManager.IsSignedIn(User)) {
  • Booking Requests - -
  • - } - @if (User.Identity.IsAuthenticated) - { -
  • - - About Me - @@ -158,7 +148,7 @@ @* ReSharper disable once Razor.SectionNotResolved *@ -@section BeforeContent { +@section BeforeContent{ Beta This is a new service - your feedback will help us to improve it. @@ -230,7 +220,7 @@ height="17" width="41"> + d="M421.5 142.8V.1l-50.7 32.3v161.1h112.4v-50.7zm-122.3-9.6A47.12 47.12 0 0 1 221 97.8c0-26 21.1-47.1 47.1-47.1 16.7 0 31.4 8.7 39.7 21.8l42.7-27.2A97.63 97.63 0 0 0 268.1 0c-36.5 0-68.3 20.1-85.1 49.7A98 98 0 0 0 97.8 0C43.9 0 0 43.9 0 97.8s43.9 97.8 97.8 97.8c36.5 0 68.3-20.1 85.1-49.7a97.76 97.76 0 0 0 149.6 25.4l19.4 22.2h3v-87.8h-80l24.3 27.5zM97.8 145c-26 0-47.1-21.1-47.1-47.1s21.1-47.1 47.1-47.1 47.2 21 47.2 47S123.8 145 97.8 145"/> All content is available under the diff --git a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Shared/_LoginPartial.cshtml b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Shared/_LoginPartial.cshtml index 78ac3e18..eb445b6c 100644 --- a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Shared/_LoginPartial.cshtml +++ b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Pages/Shared/_LoginPartial.cshtml @@ -1,19 +1,29 @@ -@using GovUk.Frontend.AspNetCore.TagHelpers +@using Microsoft.AspNetCore.Identity +@using ServiceAssessmentService.Data.Entities +@using GovUk.Frontend.AspNetCore.TagHelpers + +@inject SignInManager SignInManager +@inject UserManager UserManager diff --git a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Program.cs b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Program.cs index 9ef5b3ef..8eadab29 100644 --- a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Program.cs +++ b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/Program.cs @@ -1,37 +1,15 @@ using GovUk.Frontend.AspNetCore; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.EntityFrameworkCore; using ServiceAssessmentService.Data; using ServiceAssessmentService.Data.Entities; -using Microsoft.AspNetCore.Authentication.OpenIdConnect; -using Microsoft.Identity.Web; -using Microsoft.Identity.Web.UI; -using ServiceAssessmentService.WebApp.Models; - var builder = WebApplication.CreateBuilder(args); -var initialScopes = builder.Configuration["DownstreamApi:Scopes"]?.Split(' ') ?? - builder.Configuration["MicrosoftGraph:Scopes"]?.Split(' '); - // Add services to the container. -builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) - .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")) - .EnableTokenAcquisitionToCallDownstreamApi(initialScopes) - .AddMicrosoftGraph(builder.Configuration.GetSection("MicrosoftGraph")) - .AddInMemoryTokenCaches(); - -builder.Services.AddAuthorization(options => -{ - // By default, all incoming requests will be authorized according to the default policy. - options.FallbackPolicy = options.DefaultPolicy; -}); -builder.Services.AddRazorPages() - .AddMicrosoftIdentityUI(); - -// // Used for local accounts -// builder.Services -// .AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) -// .AddEntityFrameworkStores(); +builder.Services.AddControllersWithViews(); +builder.Services.AddRazorPages(); builder.Services.AddGovUkFrontend(); @@ -40,12 +18,23 @@ options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")); }); +builder.Services + .AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) + .AddEntityFrameworkStores(); builder.Services.AddHealthChecks() .AddDbContextCheck(); builder.Services.AddScoped(); -builder.Services.AddScoped(); + +builder.Services.AddControllers(config => +{ + // Default to requiring authorisation, unless explicit [AllowAnonymous] specified for the page/route + var policy = new AuthorizationPolicyBuilder() + .RequireAuthenticatedUser() + .Build(); + config.Filters.Add(new AuthorizeFilter(policy)); +}); builder.Services.AddApplicationInsightsTelemetry(); @@ -59,18 +48,14 @@ // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } -else -{ - app.UseDeveloperExceptionPage(); -} app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); -app.UseAuthentication(); app.UseAuthorization(); +app.UseAuthentication(); app.MapControllerRoute( name: "default", diff --git a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/ServiceAssessmentService.WebApp.csproj b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/ServiceAssessmentService.WebApp.csproj index 7ed13db5..95607369 100644 --- a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/ServiceAssessmentService.WebApp.csproj +++ b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/ServiceAssessmentService.WebApp.csproj @@ -12,11 +12,14 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - - - diff --git a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/appsettings.Development.json b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/appsettings.Development.json index 0e902a7e..3bb1769d 100644 --- a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/appsettings.Development.json +++ b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/appsettings.Development.json @@ -1,13 +1,4 @@ { - "AzureAd": { - "Instance": "https://login.microsoftonline.com/", - "Domain": "platform.education.gov.uk", - "TenantId": "9c7d9dd3-840c-4b3f-818e-552865082e16", - "ClientId": "3bccece3-7950-4793-8799-49bf7ad14ed1", - "CallbackPath": "/signin-oidc", - "ClientSecret": "Client secret from app-registration. Check user secrets/azure portal.", - "ClientCertificates": [] - }, "ConnectionStrings": { "DefaultConnection": "", "RemoteDevConnection": "Server=serviceassessmentplus-dev.database.windows.net;Database=ServiceAssessmentPlus-dev;Authentication=Active Directory Integrated;", diff --git a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/appsettings.json b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/appsettings.json index cc5fa3d5..fce100ee 100644 --- a/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/appsettings.json +++ b/src/ServiceAssessmentService/ServiceAssessmentService.WebApp/appsettings.json @@ -2,19 +2,6 @@ "ConnectionStrings": { "DefaultConnection": "" }, - "AzureAd": { - "Instance": "", - "Domain": "", - "TenantId": "", - "ClientId": "", - "CallbackPath": "", - "ClientSecret": "", - "ClientCertificates": [] - }, - "MicrosoftGraph": { - "BaseUrl": "https://graph.microsoft.com/v1.0", - "Scopes": "user.read User.ReadBasic.All" - }, "Logging": { "LogLevel": { "Default": "Information",