Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
Incorporating Courtney's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
gildedgardenia committed Nov 18, 2022
1 parent 5011d9e commit 866cf07
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/chapters/auth/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The first place to start with configuring Identity to fit the needs of the proje
Try and code along as you read more about Identity!
This page starts off with the code in the `identity-scaffolding <https://github.com/LaunchCodeEducation/CodingEventsDemo/tree/identity-scaffolding>`__ branch in ``CodingEventsDemo``.
The final code for this page is in the `identity-config <https://github.com/LaunchCodeEducation/CodingEventsDemo/tree/identity-config>`__ branch in ``CodingEventsDemo``.

If you are looking for an additional walkthrough, check out this `article <https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?source=recommendations&view=aspnetcore-6.0>`__ from Microsoft.

``Startup.cs``
--------------
Expand Down
59 changes: 52 additions & 7 deletions src/chapters/auth/scaffolding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ The process of adding Identity to an existing code base is called **scaffolding*
This page starts off with the code in the `display-tag-items <https://github.com/LaunchCodeEducation/CodingEventsDemo/tree/display-tag-items>`__ branch in ``CodingEventsDemo``.
The final code for this page is in the `identity-scaffolding <https://github.com/LaunchCodeEducation/CodingEventsDemo/tree/identity-scaffolding>`__ branch in ``CodingEventsDemo``.

.. TODO: Check package version compatibility. Asp Net Core 5.0 was causing some issues
.. Students need to check with SDK is being used by global.json and which sdks they have available. Starter code is set up to use 3.1 so they may have to generate new global.json and roll package versions to 3.1 to work with CLI tools and ensure scaffolding is successful.
Before You Start
----------------

Expand Down Expand Up @@ -80,6 +76,11 @@ When installing these packages, make sure that the versions are the same as the

With these packages installed, you are ready to go!

.. admonition:: Note

If you are a Mac user and are getting errors related to versioning with package installs because you have multiple versions of .NET on your machine, you may need to uninstall several versions of .NET.
You will find a number of approaches for uninstalling .NET online, but here is `one <https://devkimchi.com/2021/11/24/removing-dotnet-sdks-from-macos-manually/>`__ to get you started.

Scaffolding Identity in an Exisiting Project
--------------------------------------------

Expand Down Expand Up @@ -182,6 +183,37 @@ In order to use Identity, we need to change what ``EventDbContext`` extends. Cur

public class JobDbContext: IdentityDbContext<IdentityUser, IdentityRole, string>

We also need to add an additional line to ``OnModelCreating()``:

.. sourcecode:: csharp

base.OnModelCreating(modelBuilder);

With these changes made, ``EventDbContext`` will look like the following:

.. sourcecode:: csharp
:lineno-start: 13

public class EventDbContext : IdentityDbContext<IdentityUser>
{
public DbSet<Event> Events { get; set; }
public DbSet<EventCategory> Categories { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<EventTag> EventTags { get; set; }

public EventDbContext(DbContextOptions<EventDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EventTag>().HasKey(et => new { et.EventId, et.TagId });

base.OnModelCreating(modelBuilder);
}
}

You may note that we didn't add any ``DbSet`` for ``IdentityUser`` like we did for other models in the application.
This is not an oversight! With ``EventDbContext`` properly set up, we can run a migration and the database will add the appropriate tables for our authentication data.

Expand Down Expand Up @@ -247,6 +279,10 @@ Add an additional line to ``app.UseEndpoints()`` inside of ``Configure()`` in ``
These initial steps were to make sure that the application is still using ``EventDbContext`` for its connection to the database now that we have added Identity.
However, if you take a look inside the ``Areas/Identity/Data`` directory, you will find a file also called ``EventDbContext``. Delete that generated file and continue to use the one we initially created for ``CodingEvents``.

.. admonition:: Note

If you do not immediately see Identity scaffolding, that is okay! Sometimes it takes a moment to appear.

Views
^^^^^

Expand Down Expand Up @@ -286,18 +322,27 @@ If you peek inside the file, you will find these links live inside a conditional
}
</ul>

`UserManager <https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.usermanager-1?view=aspnetcore-3.1>`__ deals with the user information in the database. We can use the properties and methods to perform operations on user objects such as adding a new user or fetching user information.
`UserManager <https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.usermanager-1?view=aspnetcore-6.0>`__ deals with the user information in the database. We can use the properties and methods to perform operations on user objects such as adding a new user or fetching user information.
On line 11 in the code above, ``UserManager`` is used to fetch the signed-in user's username so we greet them by name!
`SignInManager <https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.signinmanager-1?view=aspnetcore-3.1>`__ deals with users signing in.
`SignInManager <https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.signinmanager-1?view=aspnetcore-6.0>`__ deals with users signing in.
On line 8, ``SignInManager`` is used to check if the user is signed in. If the user is signed in, then the links that will be displayed are to manage the account or log out of the account.
If the user is not signed in, then the links are to either log in or register for an account on the site.

This partial view can be placed anywhere you need it, but we recommend starting with placing it in ``_Layout.cshtml`` so that a signed-in user can easily access the necessary links from any page.
To add it to the navbar, use the following syntax:

.. sourcecode:: guess
:lineno-start: 19

<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<partial name="_LoginPartial" />
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Add">Add Job</a>
</li>
</ul>
</div>

<partial name="_LoginPartial" />

Final Steps
^^^^^^^^^^^
Expand Down
4 changes: 4 additions & 0 deletions src/chapters/auth/users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Here are some things to note:
#. If the new user row in the database is successfully created, then we want to direct the user to a page to start working.
If not, the user registration form is reloaded, just as when the validation requirements are not met.

.. admonition:: Note

If you are interested in learning more about asynchronous return types in C#, check out the `documentation <https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/async-return-types>`__.

Login
-----

Expand Down

0 comments on commit 866cf07

Please sign in to comment.