0

I have an asp net core solution with identity which I thought I had removed the section of code that included these login page links: Forgot Password, Register as a New User, Resend Email Confirmation and Use Another Service to Login (text & link) But this content is displayed and I can't figure out why?

I have 'marked' the last line of ~\repo\Views\Shared\_LoginPartial.cshtml

The code:

@using Microsoft.AspNetCore.Identity
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager

<ul class="navbar-nav">
    @if (SignInManager.IsSignedIn(User))
    {
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Kia Ora/Welcome @User.Identity.Name</a>
        </li>
        <li class="nav-item">
            <form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })">
                <button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
            </form>
        </li>
    }
    else
    {

        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login"></a>
        </li>
    }
    <!-- LoginPartial.cshtml file ends here-->
</ul>

When I launch the app using localhost, the login page has all the links, as though I never removed them. This can be seen looking at the page using Developer Tools (right-click image "Open image in new tab"

There are other repo folders, in the parent folder, with different versions. But I have been careful to open this folder in Visual Studio and double click on solutions file. There are no resources outside of this folder. How can this happen?

I believe the bootstrap container code and the HTML role attribute don't give any clues. I could be wrong.

Dev Tools Output

Dave
  • 687
  • 7
  • 15
  • 1
    Your screenshot is showing us the login view, where as your code is showing us a partial view, which is displayed in your navbar. – Marco Mar 07 '23 at 06:30
  • I have closed down and started again. Have re-built. Spent time looking through all files. It would be useful to see full paths of all files used in build. – Dave Mar 07 '23 at 06:30
  • Yes. The file is `_LoginPartial.cshtml` Which is part of the login display. This page has included the other code. Of which I have removed. – Dave Mar 07 '23 at 06:33

2 Answers2

2

I think you are getting a bit cross-eyed looking for things, that are magically there, if you don't know where to look.

The code you are showing us is the LoginPartial view, that is injected into your Navbar. If you haven't changed it, that should be in _Layout.cshtml:

<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
    <ul class="navbar-nav flex-grow-1">
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
        </li>
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
        </li>
    </ul>
    <partial name="_LoginPartial"/>
</div>

What you are showing us as a screenshot is the Login view, which ... is not there. Asp.net core automatically adds them behind the scenes as a default implementation. You can of course override them.

  • Right click your project
  • Click on "Add New Scaffolded Item"
  • Click "Add Identity"
  • Select the Login View (and any other you want to modify)

Once the process has finished, those scaffolded items should be inside your /Areas/Identity folder.

Reference: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-7.0&tabs=visual-studio

Marco
  • 22,856
  • 9
  • 75
  • 124
  • I take your point. But asking a couple more questions may have been easier before providing such a comprehensive answer. To clarify: this solution has already been deployed to Azure (without registration links etc) I have only cloned the repo & done a migration on the main database (not Identity tables) I had previously excluded folder Account from the project (~\Areas\Identity\Pages\Account) I notice that when I completely remove this folder from **this** project I get two CS0246 errors: 'SignInManager<>' ,'IdentityUser' not found. – Dave Mar 07 '23 at 18:49
  • Obviously I'm asking why did I not get this error when the Account folder was excluded from the project? – Dave Mar 07 '23 at 18:49
  • For me. The code snippet you show has my nav bar links e.g "Enter Names" & "Home" – Dave Mar 07 '23 at 18:54
  • Even when the Account folder is tucked away somewhere else in the repo. The file _ManageNav.cshtml.g.cs is still found during build! eg `repos\rollbase-rev1\obj\Debug\net5.0\Razor\temp\Account\Manage\_ManageNav.cshtml.g.cs(132,30,132,42): error CS0246` – Dave Mar 07 '23 at 18:58
  • I got it working. I am sorry I can't mark your answer as accepted because the scaffolding had already been done. In my case, after scaffolding, I had edited `~\Areas\Identity\Pages\Account\Login.cshtml` to remove all registration links. But the folder had been excluded from the project. This problem and how the default landing page was being loaded remain issues for me to think about. Thanks for your input. – Dave Mar 08 '23 at 00:19
  • 1
    By all means go ahead and add your answer for others to follow along and all the information from the comments, that should have been as additional information in the question. – Marco Mar 08 '23 at 06:14
  • Thanks for you input. You have certainly helped. I'm sorry that I've overlooked issues like excluding files. I have come back to this solution after a year away. I still feel I'm just getting my head around it. – Dave Mar 08 '23 at 19:14
0

The question stated a presumption that Microsoft Web App default registration details etc had been removed from the solution. This is what I required and this proved to be correct. The original method I used, believing to be the best, was to scaffold all identity elements (all boxes ticked on Add Identity page of scaffolding) and then edit/remove what I didn't need. The only editing I needed to do was to remove the items in the question from ~\Areas\Identity\Pages\Account\Login.cshtml

I believe the above file is linked via the file ~\Views\Shared\_LoginPartial.cshtml to provide a 'clean' login page (without registration details) This can be seen in the code section of the question.

The issues were:

  • the folder ~\Areas\Identity\Pages\Account had been temporarily excluded from the project.
  • I didn't understand where the 'default code' had come from which reintroduced the registration details back onto the login page.

I still don't understand how all the files in Account & Management folders can be excluded from the project resulting in the default login page. A manual check of all my code files would suggest this is not possible.

For anyone looking for information on scaffolding identity. I found the following useful.

Introduction to Identity on ASP.NET Core

Where are the Login and Register pages in an AspNet Core scaffolded app?

Dave
  • 687
  • 7
  • 15