{"id":1985,"date":"2021-06-18T12:00:00","date_gmt":"2021-06-18T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/build-a-complete-app-with-asp-net-c-sql-lite-part-9\/"},"modified":"2026-07-05T03:25:59","modified_gmt":"2026-07-05T01:25:59","slug":"build-a-complete-app-with-asp-net-c-sql-lite-part-9","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/build-a-complete-app-with-asp-net-c-sql-lite-part-9\/","title":{"rendered":"Build a Complete App with ASP.Net, C#, SQL Lite \u2013 Part 9"},"content":{"rendered":"<p>In this part we would cover user registration, user login and profile. We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Add Firstname and Lastname Fields<\/a><\/li>\n<li><a href=\"#t2\">Re-Add the Migration<\/a><\/li>\n<li><a href=\"#t3\">Add Protected Content<\/a><\/li>\n<li><a href=\"#t4\">Disable Some Constraints<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">1. Add Firstname and Lastname Fields<\/strong><\/h5>\n<p>First, we are going to modify the modify the registration page to add fields for Firstname and Lastname. Next we configure and test the user registration and login function.<\/p>\n<p><strong>Step 1<\/strong> &#8211; Open the ApplicationUser.cs field which is inside the Data folder. Modify it to add the following lines:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ApplicationUser<\/span> : IdentityUser\r\n{\r\n<span style=\"color: #0000cc;\">    [PersonalData]<\/span>\r\n<span style=\"color: #0000cc;\">    [Column(TypeName =\"nvarchar(50)\")]<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span> FirstName { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }\r\n\r\n<span style=\"color: #0000cc;\">    [PersonalData]<\/span>\r\n<span style=\"color: #0000cc;\">    [Column(TypeName = \"nvarchar(50)\")]<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span> LastName { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong>\u00a0&#8211; Open the Register.cshtml.cs file and modify the InputModel to include Firstname and Lastname. The code snipped is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000cc;\">[Required]<\/span>\r\n<span style=\"color: #0000cc;\">[DataType(DataType.Text)]<\/span>\r\n<span style=\"color: #0000cc;\">[Display(Name = \"First name\")]<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span> FirstName { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }\r\n\r\n<span style=\"color: #0000cc;\">[Required]<\/span>\r\n<span style=\"color: #0000cc;\">[DataType(DataType.Text)]<\/span>\r\n<span style=\"color: #0000cc;\">[Display(Name = \"Last name\")]<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">string<\/span> LastName { <span style=\"color: #008800; font-weight: bold;\">get<\/span>; <span style=\"color: #008800; font-weight: bold;\">set<\/span>; }\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 1<\/strong> &#8211; Modify the new user line to include Firstname and Lastname inside the onPostAsync function. The line is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">var<\/span> user = <span style=\"color: #008800; font-weight: bold;\">new<\/span> ApplicationUser { UserName = Input.Email, Email = Input.Email };\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong> &#8211; Open the Register.cshtml registration form and add two additional field for Firstname and Lastname. You can simply duplicate the fields for email and adjust. The code you&#8217;ll add is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"form-group\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;label<\/span> <span style=\"color: #0000cc;\">asp-for=<\/span><span style=\"background-color: #fff0f0;\">\"Input.FirstName\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/label&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;input<\/span> <span style=\"color: #0000cc;\">asp-for=<\/span><span style=\"background-color: #fff0f0;\">\"Input.FirstName\"<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"form-control\"<\/span> <span style=\"color: #007700;\">\/&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;span<\/span> <span style=\"color: #0000cc;\">asp-validation-for=<\/span><span style=\"background-color: #fff0f0;\">\"Input.FirstName\"<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"text-danger\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/span&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"form-group\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;label<\/span> <span style=\"color: #0000cc;\">asp-for=<\/span><span style=\"background-color: #fff0f0;\">\"Input.LastName\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/label&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;input<\/span> <span style=\"color: #0000cc;\">asp-for=<\/span><span style=\"background-color: #fff0f0;\">\"Input.LastName\"<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"form-control\"<\/span> <span style=\"color: #007700;\">\/&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;span<\/span> <span style=\"color: #0000cc;\">asp-validation-for=<\/span><span style=\"background-color: #fff0f0;\">\"Input.LastName\"<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"text-danger\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/span&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">2. Re-Add the Migrations<\/strong><\/h5>\n<p><strong>Step 5<\/strong> &#8211; Delete the AuthData migration.<\/p>\n<p><strong>Step 6<\/strong> &#8211; Also delete the Database because we&#8217;ll re-create it<\/p>\n<p><strong>Step 7<\/strong>\u00a0&#8211; Add again the AuthData migration. You should be able to do this <a href=\"https:\/\/www.kindsonthegenius.com\/build-a-complete-app-with-asp-net-c-sql-lite-part-4\/\" target=\"_blank\" rel=\"noopener\">from Part 4<\/a>.<\/p>\n<p><strong>Step 8<\/strong>\u00a0&#8211; Update the database using the HospitalContext<\/p>\n<p><strong>Step 9<\/strong>\u00a0&#8211; Update the database again using the AuthContext<\/p>\n<p>Now you can launch the application and visit the register page. Create a user and see how it all works.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. Add Protected Content<\/strong><\/h5>\n<p>Now we would like to have the user be logged in before he can access the home page. To do this, follow the steps below:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Open the HomeController.cs file and annotate the class with the [Authorize] annotation.<\/p>\n<p>Now when you launch the application, it would redirect to the Login page. So you can register a user and then log in.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t4\">4. Disable Some Constraints (optional)<\/strong><\/h5>\n<p>Once you register, you will notice that you are redirected to an email confirmation page. We would like to disable that since we don&#8217;t have email confirmation yet.<\/p>\n<p>We can also disable password strength feature since this is just a demo. To do these, follow the steps below:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Open the IdentityHostingStartup.cs file and adjust the options parameter of the AddDefaultIdentity() like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">services.AddDefaultIdentity&lt;ApplicationUser&gt;(options =&gt; {\r\n    options.SignIn.RequireConfirmedAccount = <span style=\"color: #008800; font-weight: bold;\">true<\/span>;\r\n    options.SignIn.RequireConfirmedEmail = <span style=\"color: #008800; font-weight: bold;\">false<\/span>;\r\n    options.Password.RequireUppercase = <span style=\"color: #008800; font-weight: bold;\">false<\/span>;\r\n})\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>There are much that can be done on authentication and authorization in .Net Core. However, I&#8217;m making another complete tutorial that covers ASP.NET Authentication and Authorization. For now, we would just continue the tutorial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this part we would cover user registration, user login and profile. We would cover the following: Add Firstname and Lastname Fields Re-Add the Migration &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[311],"tags":[],"class_list":["post-1985","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1985","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1985"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1985\/revisions"}],"predecessor-version":[{"id":2153,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1985\/revisions\/2153"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}