Skip to content

Commit

Permalink
not adding/forcing Account.Logout.cshtml if blazor project already ad…
Browse files Browse the repository at this point in the history
…ds it. (#1721)
  • Loading branch information
deepchoudhery committed Dec 13, 2021
1 parent c05737b commit 4099e44
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,5 +11,7 @@ public class IdentityGeneratorTemplateModel2 : IdentityGeneratorTemplateModel
public string ContentVersion { get; set; }

public bool IsRazorPagesProject { get; set; }

public bool IsBlazorProject { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ public async Task<IdentityGeneratorTemplateModel> ValidateAndBuild()
SupportFileLocation = supportFileLocation,
HasExistingNonEmptyWwwRoot = HasExistingNonEmptyWwwRootDirectory,
BootstrapVersion = boostrapVersion,
IsRazorPagesProject = IsRazorPagesLayout()
IsRazorPagesProject = IsRazorPagesLayout(),
IsBlazorProject = IsBlazorProjectLayout()
};

templateModel.ContentVersion = DetermineContentVersion(templateModel);
Expand Down Expand Up @@ -259,11 +260,10 @@ public async Task<IdentityGeneratorTemplateModel> ValidateAndBuild()
{
ValidateFilesOption(templateModel);
}

return templateModel;
}

private IdentityGeneratorFile[] DetermineFilesToGenerate(IdentityGeneratorTemplateModel templateModel)
private IdentityGeneratorFile[] DetermineFilesToGenerate(IdentityGeneratorTemplateModel2 templateModel)
{
var filesToGenerate = new List<IdentityGeneratorFile>(IdentityGeneratorFilesConfig.GetFilesToGenerate(NamedFiles, templateModel));

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4099e44

Please sign in to comment.