diff --git a/src/chapters/auth/configuration.rst b/src/chapters/auth/configuration.rst index 3c34e22d..5d6fc19e 100644 --- a/src/chapters/auth/configuration.rst +++ b/src/chapters/auth/configuration.rst @@ -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 `__ branch in ``CodingEventsDemo``. The final code for this page is in the `identity-config `__ branch in ``CodingEventsDemo``. - + If you are looking for an additional walkthrough, check out this `article `__ from Microsoft. ``Startup.cs`` -------------- diff --git a/src/chapters/auth/scaffolding.rst b/src/chapters/auth/scaffolding.rst index 9a818597..58ed8a3b 100644 --- a/src/chapters/auth/scaffolding.rst +++ b/src/chapters/auth/scaffolding.rst @@ -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 `__ branch in ``CodingEventsDemo``. The final code for this page is in the `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 ---------------- @@ -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 `__ to get you started. + Scaffolding Identity in an Exisiting Project -------------------------------------------- @@ -182,6 +183,37 @@ In order to use Identity, we need to change what ``EventDbContext`` extends. Cur public class JobDbContext: IdentityDbContext +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 + { + public DbSet Events { get; set; } + public DbSet Categories { get; set; } + public DbSet Tags { get; set; } + public DbSet EventTags { get; set; } + + public EventDbContext(DbContextOptions options) + : base(options) + { + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().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. @@ -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 ^^^^^ @@ -286,9 +322,9 @@ If you peek inside the file, you will find these links live inside a conditional } -`UserManager `__ 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 `__ 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 `__ deals with users signing in. +`SignInManager `__ 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. @@ -296,8 +332,17 @@ This partial view can be placed anywhere you need it, but we recommend starting To add it to the navbar, use the following syntax: .. sourcecode:: guess + :lineno-start: 19 + + - Final Steps ^^^^^^^^^^^ diff --git a/src/chapters/auth/users.rst b/src/chapters/auth/users.rst index 6e87cfab..043bb8d1 100644 --- a/src/chapters/auth/users.rst +++ b/src/chapters/auth/users.rst @@ -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 `__. + Login -----