{"id":326,"date":"2024-04-03T00:36:23","date_gmt":"2024-04-02T23:36:23","guid":{"rendered":"https:\/\/davagordon.co.uk\/blog\/?p=326"},"modified":"2024-05-12T02:31:17","modified_gmt":"2024-05-12T01:31:17","slug":"setting-up-a-net-mvc-app-with-identity-user-authentication-how-to","status":"publish","type":"post","link":"https:\/\/davagordon.co.uk\/blog\/setting-up-a-net-mvc-app-with-identity-user-authentication-how-to\/","title":{"rendered":"Setting Up a .NET MVC App with Identity User Authentication &#8211; How To"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll walk you through the steps of creating a.NET MVC (Model-View-Controller) application from scratch. MVC is a popular architectural pattern for developing web applications that offers an organised approach to separating concerns and managing application logic.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Create a New MVC Project<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Start by launching Visual Studio and creating a new project. If you don&#8217;t already have it, you can <a href=\"https:\/\/visualstudio.microsoft.com\/free-developer-offers\/\" target=\"_blank\" rel=\"noopener nofollow\" title=\"Download Visual Studio\">download it here<\/a>.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open Visual Studio.<\/li>\n\n\n\n<li>Go to <code>File > New > Project<\/code>.<\/li>\n\n\n\n<li>Choose <code>ASP.NET Web Application<\/code>.<\/li>\n\n\n\n<li>Select <code>MVC<\/code> as the project template.<\/li>\n\n\n\n<li>Click <code>Create<\/code> to generate the project structure.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Install Entity Framework Core<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Entity Framework Core is an Object-Relational Mapper (ORM) that makes database interactions easier in.NET applications. Here&#8217;s how you install it:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open the <code>NuGet Package Manager Console<\/code>.<\/li>\n\n\n\n<li>Run the below commands:<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nInstall-Package Microsoft.EntityFrameworkCore.SqlServer\nInstall-Package Microsoft.EntityFrameworkCore.Tools\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Step 3: Set Up Identity for User Authentication<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s add Identity to our MVC application to manage user authentication and authorization.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3.1: Install Identity Framework<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Identity is a membership system that integrates user authentication and authorization features in your application. Install it via the NuGet Package Manager Console:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nInstall-Package Microsoft.AspNetCore.Identity.EntityFrameworkCore\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Step 3.2: Create Database Context<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a class that inherits from IdentityDbContext to represent the application&#8217;s database context. This class defines the tables used by Identity.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing Microsoft.AspNetCore.Identity.EntityFrameworkCore;\nusing Microsoft.EntityFrameworkCore;\n\npublic class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;\n{\n    public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options)\n        : base(options)\n    {\n    }\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Step 3.3: Configure Identity Services<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In the <code>Program.cs<\/code> file, configure Identity services by adding the necessary services to the service container.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nbuilder.Services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;\n    options.UseSqlServer(connectionString));\n\nbuilder.Services.AddDefaultIdentity&lt;IdentityUser&gt;(options =&gt; options.SignIn.RequireConfirmedAccount = true)\n    .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;();\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Step 3.4: Set Up User Model<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a class that represents the user model. This class should inherit from <strong>IdentityUser<\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nusing Microsoft.AspNetCore.Identity;\n\npublic class ApplicationUser : IdentityUser\n{\n    \/\/ Additional properties if needed\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Step 4: Apply Migrations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use Entity Framework migrations to create the necessary database schema for Identity.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ndotnet ef migrations add InitialCreate\ndotnet ef database update\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Step 5: Configure Authentication<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the <code>Program.cs<\/code>, just above <code>app.Run();<\/code> add the authentication middleware.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napp.UseAuthentication();\napp.UseAuthorization();\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Step 6: Create Views and Controllers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create views and controllers for user registration, login, logout, and other authentication related actions as needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 7: Secure Controller Actions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the [Authorise] attribute to safeguard controller operations that need authentication.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You have successfully configured a.NET MVC application with Identity user authentication. You can now register users, authenticate them, and restrict access to different portions of your application based on their responsibilities. Feel free to tailor the setup and UI to your application&#8217;s needs. if you enjoyed this article why not check out <a href=\"https:\/\/davagordon.co.uk\/blog\/category\/web-development\/\" title=\"Web Development\">similar articles.<\/a>  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we&#8217;ll walk you through the steps of creating a.NET MVC (Model-View-Controller) application from scratch. MVC is a popular architectural pattern for developing web applications that offers an organised approach to separating concerns and managing application logic. Step 1: Create a New MVC Project Start by launching Visual Studio and creating a new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":329,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[24,45,46],"class_list":["post-326","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-net","tag-identity","tag-microsoft"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/posts\/326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=326"}],"version-history":[{"count":3,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/posts\/326\/revisions"}],"predecessor-version":[{"id":330,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/posts\/326\/revisions\/330"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/media\/329"}],"wp:attachment":[{"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davagordon.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}