Results 1 to 1 of 1

Thread: Seed Roles and Users to an existing database in ASP.NET MVC 5 using Identity

  1. #1

    Thread Starter
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Seed Roles and Users to an existing database in ASP.NET MVC 5 using Identity

    Hello forum friends,

    In this tutorial, I will demonstrate seeding roles and users to an existing database in an ASP.NET MVC 5 VB.NET application using Identity framework. For simplicity, I wil use a Northwind database or you may choose another database aside from this. This database doesn't have membership tables at all.

    Name:  1 DB No Membership Tables.PNG
Views: 3685
Size:  14.4 KB

    To begin with, accomplish the steps below:
    1. Create an ASP.NET MVC project (VB) and then change web.config connectionStrings element to connect to an
    existing database (Northwind as my example).
    2. In Package Manager Console execute command
    PM> Enable-Migrations
    3. In Package Manager Console execute command
    PM> Add-Migration ASPMembership
    * This will create a file Timespan_ASPMembership.vb inside Migrations folder with scripts to create Membership
    Tables such as AspNetUsers, AspNetRoles and etc..
    4. In Package Manager Console execute command
    PM> Update-Database
    * This will add membership tables to Northwind database.
    Name:  2 DB With Membership Tables.PNG
Views: 3619
Size:  20.4 KB
    5. To seed Roles and Users, create a class SeedRolesAndUsers.vb inside Models folder. This class references
    Microsoft.AspNet.Identity and Microsoft.AspNet.Identity.EntityFramework namespaces. The Seed() method
    adds Customer or Manager role if they don't exist. The remaining part of the code creates a user and assign it
    to a specific role.

    VB.NET Code:
    1. Public Class SeedRolesAndUsers
    2.  
    3.     Public Shared Sub Seed(context As ApplicationDbContext)
    4.         Dim userManager = New UserManager(Of ApplicationUser)(New UserStore(Of ApplicationUser)(context))
    5.         Dim roleManager = New RoleManager(Of IdentityRole)(New RoleStore(Of IdentityRole)(context))
    6.  
    7.         If Not roleManager.RoleExists("Customer") Then
    8.             Dim roleresult = roleManager.Create(New IdentityRole("Customer"))
    9.         End If
    10.         If Not roleManager.RoleExists("Manager") Then
    11.             Dim roleresult = roleManager.Create(New IdentityRole("Manager"))
    12.         End If
    13.  
    14.         Dim userName As String = "jimmymgr01"
    15.         Dim password As String = "jimmymgr01"
    16.         Dim user As ApplicationUser = userManager.FindByName(userName)
    17.         If user Is Nothing Then
    18.  
    19.             user = New ApplicationUser() With {
    20.                 .UserName = userName
    21.             }
    22.  
    23.             Dim userResult As IdentityResult = userManager.Create(user, password)
    24.             If userResult.Succeeded Then
    25.                 Dim result = userManager.AddToRole(user.Id, "Manager")
    26.             End If
    27.         End If
    28.     End Sub
    29.  
    30. End Class

    6. In Global.asax.vb Application_Start() method, call the seed method and then pass an ApplicationDBContext object to it.

    VB.NET Code:
    1. Dim context = New ApplicationDbContext()
    2.  SeedRolesAndUsers.Seed(context)

    7. Run the application. You should see the tables AspNetRoles, AspNetUsers, AspNetUserRoles populated with data.
    Name:  3 Roles Users and UserRoles.jpg
Views: 3533
Size:  23.4 KB

    That's it.. :-)
    Last edited by KGComputers; Oct 26th, 2016 at 02:03 AM.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width