Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not adding/forcing Account.Logout.cshtml if blazor project already adding it #1721

Merged
merged 1 commit into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
deepchoudhery marked this conversation as resolved.
Show resolved Hide resolved
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