I have a webapp using the VS template which includes pages that create users etc. I have created users and they can/can not access folders according to the web.config in the various folders.
Now I need to add roles and attach roles to users.
I can create roles using
Dim createRole As String = RoleTextBox.Text Dim RoleManager = New RoleManager(Of IdentityRole)(New RoleStore(Of IdentityRole)(New ApplicationDbContext())) Try If RoleManager.RoleExists(createRole) Then Msg.Text = "Role '" & Server.HtmlEncode(createRole) & "' already exists. Please specify a different role name." Return End If RoleManager.Create(New IdentityRole(createRole))
Next I need to attach roles to users, for which I have got:
Public Class admin_superadmin Inherits System.Web.UI.Page Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim UserManager = New UserManager(Of IdentityUser)(New UserStore(Of IdentityUser)(New IdentityDbContext())) UserManager.AddToRole(DropDownList5.SelectedValue, DropDownList6.SelectedValue) Label1.Text = DropDownList5.SelectedItem.Text + " Was added to the " + DropDownList6.SelectedValue + " Role." End Sub
However when run this fails at the line usermanager.addtorole with the message: System.InvalidOperationException: UserId not found.
The DB tables were created by the app. In table AspNetUserRoles are fields UserID, RoleID.
Much appreciated if someone could help identify the problem.