Blog

Creating a new .NET Core project with authorisation


Watch out - there are lots of similarly-named pages and plenty of places to go wrong.

Start here, with "Create a Web app with authentication": https://docs.microsoft.com/en-gb/aspnet/core/security/authentication/identity?view=aspnetcore-3.1&tabs=visual-studio

After initial setup, you'll need to Scaffold the Register, Login and LogOut functions: https://docs.microsoft.com/en-gb/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-3.1&tabs=visual-studio#scaffold-identity-into-a-razor-project-with-authorization - Override all files and choose the data context class you created in step 1. You'll see a lot of new pages in Areas/Identity/Pages/Account.

Sign up for SendGrid, and follow the instructions in https://docs.microsoft.com/en-gb/aspnet/core/security/authentication/accconfirm?view=aspnetcore-3.1&tabs=visual-studio#prevent-login-at-registration. You'll also need to make DNS changes to set up domain authentication, so your emails don't go straight to spam: https://sendgrid.com/docs/ui/account-and-settings/how-to-set-up-domain-authentication/.

If you'd prefer to use a local SQL Server instance instead of MSSQLLocalDB, now is a good time to move the tables over (while they're empty!). Create a new databae in your local SQL Server, use MSSQLLocalDB to generate create scripts and run them on your local SQL Server. Change the "DefaultConnection" string on appsettings.json to point at your local server.

Next I set up a number of data tables for editing using Razor pages, using the instructions at https://docs.microsoft.com/en-gb/aspnet/core/data/ef-rp/intro?view=aspnetcore-3.1&tabs=visual-studio. You'll have to define the data structure rather than being able to use the database first model, but it works fine. 

If you're feeling keen, you'll want to use your database to define the navigation menu. Don't think about trying to do this with a Partial View, save yourself the pain and head straight for View Components. https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-3.1#walkthrough-creating-a-simple-view-component has a good walkthrough.