Search code examples
asp.netasp.net-mvcvb.netasp.net-identity

How to implement the Identity Roles Manager in MVC


I'm very new to MVC (as in this is my first project working in it, I apologize for any misguided or bad explanations) and I am not fully understanding how to make a user role. I know there are lots of these questions on here but none of them have been able to help my understanding of the role manager.

I am using ASP NET identity to make my users, edit them, the whole deal. I have all the Identity generated tables in my database with all their relationships. Now, I can make users but I have no real idea with how to get those users roles.

I figured that roles were just a part of the Account model/controller, like login and registration, where I just needed to alter a few lines of code to get it to work in my project with my own db. But I can barely find any code files that have a mention of roles. Anything I've seen through Google has just flown over my head (namely since that code first and seeding stuff is a little much for me to swallow as an admitted newbie and someone who added a database and used the code-first generator in visual studio)

Is there something in the Account or Identity files controller/model I need to alter or write in or is this like creating CRUD actions for my own tables where I make a controller based on my model and add in roles from there?

I am using Identity 2.0 with MVC 4.5. Thanks in advance for the help!

---- Edit ------

I've done a bunch more Googling and I think that when I made my project I wasn't given the code behind for RoleController or ApplicationRoleManager, etc. Is this normal or did I set something up wrong?


Solution

  • I managed to figure out my problem and with a lot of trying to convert c# into vb here we go!

    I ended up seeding my roles into my database. In the startup.vb---in the root directory not the views directory---write the following:

    Imports Owin
    Imports Microsoft.Owin
    Imports Microsoft.AspNet.Identity
    Imports Microsoft.AspNet.Identity.EntityFramework
    
    <Assembly: OwinStartupAttribute(GetType(Startup))>
    
    Partial Public Class Startup
        Public Sub Configuration(app As IAppBuilder)
            ConfigureAuth(app)
            createRoles()
        End Sub
    
        Private Sub createRoles()
            Dim UserManager As New UserManager(Of ApplicationUser)(New UserStore(Of ApplicationUser)(New ApplicationDbContext))
            Dim RoleManager As New RoleManager(Of IdentityRole)(New RoleStore(Of IdentityRole)(New ApplicationDbContext))
            Dim role = New IdentityRole()
    
            If Not RoleManager.RoleExists("Administration") Then
                role.Name = "Administration"
                RoleManager.Create(role)
    
                UserManager.AddToRole("user id", "Administration")
    
            End If
    
            If Not RoleManager.RoleExists("User") Then
                role.Name = "User"
                RoleManager.Create(role)
    
                UserManager.AddToRole("user Id", "User")
            End If
        End Sub
    End Class
    

    I added in a new sub procedure to my Startup class to create my roles (administration and user). In addition, the two imports of Identity and Identity.EntityFramework need to be added to the file for this to work.

    In the createRoles Sub, we need to define a new UserManager (to add the roles to users) and RoleManager (to add roles to the db). Inside of the two Managers, the 'ApplicationDbContext' will be replaced with your DbContext (which is found in the IdentityModel in the Model folder) After that we need to add a new IdentityRole object, also so we can add a role.

    Next, we check if a certain role exists 'administration', it doesn't exist so we add a name to the the role object and use the RoleManager to create it. Since I already have users in my database I can use the UserManager to add the user a role. I grabbed my user id from the database and wrote that in and then the name of the role.

    I do this again for the other role I want to add 'User'. Build the project and volia! You can users with roles and now you can apply those roles to your page controller to have pages only accessed by 'administration' or 'user'.

    I commented out the call to the function after I ran it so it wouldn't call it every build but since those roles exist it won't add anymore

    You can apply those role to your pages in your controllers like so:

    <Authorize(Roles:="Administration")>
    Public Function yourPage() as ActionResult
        Return View()
    End Function
    
    <Authorize(Roles:="Administration, User")>
    Public Function yourPage2() as ActionResult
        Return View()
    End Function
    

    I hope this can help someone else out in the future!