From eb3690d6a4968c3155d25769b95b71d9185fc877 Mon Sep 17 00:00:00 2001 From: deepchoudhery Date: Tue, 7 Dec 2021 14:27:59 -0800 Subject: [PATCH] not adding/forcing Account.Logout.cshtml if blazor project already adds it. --- .../IdentityGeneratorTemplateModel2.cs | 4 +- .../IdentityGeneratorTemplateModelBuilder.cs | 48 +++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Scaffolding/VS.Web.CG.Mvc/Identity/IdentityGeneratorTemplateModel2.cs b/src/Scaffolding/VS.Web.CG.Mvc/Identity/IdentityGeneratorTemplateModel2.cs index 7bab13bc1..bbea7bb6f 100644 --- a/src/Scaffolding/VS.Web.CG.Mvc/Identity/IdentityGeneratorTemplateModel2.cs +++ b/src/Scaffolding/VS.Web.CG.Mvc/Identity/IdentityGeneratorTemplateModel2.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. namespace Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Identity @@ -11,5 +11,7 @@ public class IdentityGeneratorTemplateModel2 : IdentityGeneratorTemplateModel public string ContentVersion { get; set; } public bool IsRazorPagesProject { get; set; } + + public bool IsBlazorProject { get; set; } } } diff --git a/src/Scaffolding/VS.Web.CG.Mvc/Identity/IdentityGeneratorTemplateModelBuilder.cs b/src/Scaffolding/VS.Web.CG.Mvc/Identity/IdentityGeneratorTemplateModelBuilder.cs index 55bd44afb..d10ad4b93 100644 --- a/src/Scaffolding/VS.Web.CG.Mvc/Identity/IdentityGeneratorTemplateModelBuilder.cs +++ b/src/Scaffolding/VS.Web.CG.Mvc/Identity/IdentityGeneratorTemplateModelBuilder.cs @@ -207,7 +207,8 @@ public async Task ValidateAndBuild() SupportFileLocation = supportFileLocation, HasExistingNonEmptyWwwRoot = HasExistingNonEmptyWwwRootDirectory, BootstrapVersion = boostrapVersion, - IsRazorPagesProject = IsRazorPagesLayout() + IsRazorPagesProject = IsRazorPagesLayout(), + IsBlazorProject = IsBlazorProjectLayout() }; templateModel.ContentVersion = DetermineContentVersion(templateModel); @@ -259,11 +260,10 @@ public async Task ValidateAndBuild() { ValidateFilesOption(templateModel); } - return templateModel; } - private IdentityGeneratorFile[] DetermineFilesToGenerate(IdentityGeneratorTemplateModel templateModel) + private IdentityGeneratorFile[] DetermineFilesToGenerate(IdentityGeneratorTemplateModel2 templateModel) { var filesToGenerate = new List(IdentityGeneratorFilesConfig.GetFilesToGenerate(NamedFiles, templateModel)); @@ -279,6 +279,18 @@ private IdentityGeneratorFile[] DetermineFilesToGenerate(IdentityGeneratorTempla } var filesToGenerateArray = filesToGenerate.ToArray(); + + //Blazor projects with Individual Auth enabled ship with a custom Account\Logout.cshtml file. If found, don't add the Account\Logout template shipped with dotnet/Scaffolding (based on aspnetcore\Identity's template). + if (templateModel.IsBlazorProject) + { + string logoutFilePath = $"{_applicationInfo.ApplicationBasePath}\\Areas\\Identity\\Pages\\Account\\LogOut.cshtml"; + if (File.Exists(logoutFilePath)) + { + //remove Account\Logout.cshtml and Account\Logout.cshtml.cs files. This is not super performant but doesn't need to be. + filesToGenerateArray = filesToGenerateArray.Where(x => !x.Name.Contains("Account.Logout")).ToArray(); + } + } + ValidateFilesToGenerate(filesToGenerateArray); return filesToGenerateArray; @@ -420,6 +432,36 @@ internal bool IsRazorPagesLayout() return Directory.Exists(pagesFilesCheckPath); } + + internal bool IsBlazorProjectLayout() + { + bool isBlazorProject = false; + string programCsFile = Path.Combine(_applicationInfo.ApplicationBasePath, "Program.cs"); + if (!File.Exists(programCsFile)) + { + programCsFile = Directory.EnumerateFiles(_applicationInfo.ApplicationBasePath, "Program.cs").FirstOrDefault(); + } + + //check for Blazor server + if (!string.IsNullOrEmpty(programCsFile)) + { + string programCsText = File.ReadAllText(programCsFile); + if (!string.IsNullOrEmpty(programCsText) && programCsText.Contains("AddServerSideBlazor()")) + { + isBlazorProject = true; + } + } + + //check for blazor wasm + if (!isBlazorProject && + _projectContext.PackageDependencies.Any(p => p.Name.Equals("Microsoft.AspNetCore.Components.WebAssembly", StringComparison.Ordinal))) + { + isBlazorProject = true; + } + + return isBlazorProject; + } + private void ValidateFilesToGenerate(IdentityGeneratorFile[] filesToGenerate) { var rootPath = _applicationInfo.ApplicationBasePath;