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 .

  1. Scaffolding
  2. Configuration
  3. Authorization

The Starter Code

  1. Fork and clone the starter code for TechJobsAuthentication .

  2. You will need to do some work to ensure that the user, and database password match your own local MySQL setup.

  3. Open Program.cs and update the following code:

    var connectionString = "server=localhost;user=your_username;password=your_password;database=techjobs_auth";
  4. 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
Note

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

  1. In the project you have cloned, scaffold Identity onto the codebase.

    • Use the same files as the chapter content and JobDbContext.
  2. Update Program.cs and JobDbContext as necessary.

  3. Add the LoginPartial partial view to the navbar.

  4. Run a new migration and test the application.

Configuration

  1. 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.
  2. Complete any additional necessary configuration steps in Program.cs.

Authorization

  1. Add the necessary attributes so only logged-in users can add jobs, but all visitors to the application can see the listing of jobs.
Note

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!