Studio: TechJobs Authentication
For this studio, you’ll be tasked with adding simple user authentication to your TechJobs
application. The steps to do this will match what you have already done in CodingEvents
. You should refer back to the tutorial starting
here
.
The Starter Code
Fork and clone the starter code for TechJobsAuthentication .
You will need to do some work to ensure that the user, and database password match your own local MySQL setup.
Open
Program.cs
and update the following code:var connectionString = "server=localhost;user=your_username;password=your_password;database=techjobs_auth";
Before getting started with setting up Identity, run a new migration to make sure that all of the database info is correct.
dotnet ef migrations add new-migration
dotnet ef database update
We’ve greatly reduced the functionality of the app so you can focus on the work to set up authentication. Running the application now
gives you a familiar-looking navbar with one menu option, Add Jobs. You can add jobs right away and an astute observer of the starter code and schema tables will notice that the fields on Job
are only strings, not complex objects.
Scaffolding
In the project you have cloned, scaffold Identity onto the codebase.
- Use the same files as the chapter content and
JobDbContext
.
- Use the same files as the chapter content and
Update
Program.cs
andJobDbContext
as necessary.Add the
LoginPartial
partial view to the navbar.Run a new migration and test the application.
Configuration
Use the appropriate methods to set the following validation conditions on the password:
- The password must be at a minimum of 8 characters.
- The password does NOT need to contain an uppercase letter.
Complete any additional necessary configuration steps in
Program.cs
.
Authorization
- Add the necessary attributes so only logged-in users can add jobs, but all visitors to the application can see the listing of jobs.
You will need to implement the using Microsoft.AspNetCore.Authorization;
inside of your Controller class in order for the [Authorize] and [AllowAnonymous] to work properly.
That’s it, that’s all. You’re done. Go forth and test the auth flow by visiting the home page, registering for a new account, and logging into and out of an existing account. Then add this to any other ASP.NET project you’re working on!