{"id":43,"date":"2020-02-04T10:13:34","date_gmt":"2020-02-04T10:13:34","guid":{"rendered":"https:\/\/aabirtech.com\/shiblenomanblog\/?p=43"},"modified":"2020-10-12T10:52:33","modified_gmt":"2020-10-12T10:52:33","slug":"asp-net-core-seed-user-and-role","status":"publish","type":"post","link":"https:\/\/aabirtech.com\/shiblenomanblog\/2020\/02\/04\/asp-net-core-seed-user-and-role\/","title":{"rendered":"ASP.Net Core Seed User and Role"},"content":{"rendered":"\n<p>Data seeding for Identity user tables are different. Here is an example code how to seed data without extending the Identity User table. First step to create a class in Data folder Name it &#8220;ApplicationDbInitializer.cs&#8221;, following code will be in this class.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using Microsoft.AspNetCore.Identity;\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Threading.Tasks;\n\nnamespace AppName.Data\n{\n    public class ApplicationDbInitializer\n    {\n        public static void SeedUsers(UserManager&lt;IdentityUser> userManager, RoleManager&lt;IdentityRole> roleManager)\n        {\n            string admin = \"Admin\";\n            string normalUser = \"NormalUser\";\n            string password = \"p@55w0rD\"; \n\n            if (roleManager.FindByNameAsync(\"Admin\").Result == null)\n            {\n                IdentityRole role = new IdentityRole\n                {\n                    Name = admin,\n                    NormalizedName = \"ADMIN\"\n                };\n\n                roleManager.CreateAsync(role).Wait();\n            }\n\n            if (roleManager.FindByNameAsync(\"NormalUser\").Result == null)\n            {\n                IdentityRole role1 = new IdentityRole\n                {\n                    Name = normalUser,\n                    NormalizedName = \"NORMALUSER\"\n                };\n\n                roleManager.CreateAsync(role1).Wait();                \n            }\n\n            if (userManager.FindByEmailAsync(\"admin@domain.com\").Result == null)\n            {\n                IdentityUser user = new IdentityUser\n                {\n                    UserName = \"admin@domain.com\",\n                    Email = \"admin@domain.com\"\n                };\n\n                IdentityResult result = userManager.CreateAsync(user, password).Result;\n\n                if (result.Succeeded)\n                {\n                    userManager.AddToRoleAsync(user, admin).Wait();\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>Second step, call above class from Startup.cs file Configure method. Add this line at bottom of the Configure method. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ApplicationDbInitializer.SeedUsers(userManager, roleManager);<\/code><\/pre>\n\n\n\n<p>Add dependency injection in Configure method arguments like the code below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void Configure(IApplicationBuilder app, IWebHostEnvironment env, UserManager&lt;IdentityUser> userManager, RoleManager&lt;IdentityRole> roleManager)<\/code><\/pre>\n\n\n\n<p>Add these two string &#8220;UserManager userManager, RoleManager roleManager&#8221; in Configure method argument. <\/p>\n\n\n\n<p>In the Startup.cs file need to update ConfigureServices method, create a new service for Identity. Add the following code in ConfigureServices method &#8211; <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>services.AddDefaultIdentity&lt;IdentityUser>().AddRoles&lt;IdentityRole>().AddEntityFrameworkStores&lt;AppDbContext>();<\/code><\/pre>\n\n\n\n<p>Once you run the application, it should create a user, role and assign the user in role in Identity User table. <\/p>\n\n\n\n<p>Happy Coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data seeding for Identity user tables are different. Here is an example code how to seed data without extending the Identity User table. First step to create a class in Data folder Name it &#8220;ApplicationDbInitializer.cs&#8221;, following code will be in this class. Second step, call above class from Startup.cs file Configure method. Add this line [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,5],"tags":[6,7,8,11],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-asp-net-core","category-identity","tag-asp-core","tag-core-3-0","tag-core-3-1","tag-identity"],"acf":[],"_links":{"self":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":3,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts\/43\/revisions\/71"}],"wp:attachment":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}