Answered by:
Cannot create user/roles in ASP Identity

Question
-
User-626985523 posted
- Web Forms project / VB / ASP 4.5.2 / VS2015
- EntityFramework 6.1.3 / Identity.EntityFramework 2.2.1
- Other packages in NuGet all show current versions
I have a webapp using the VS template which includes pages that create users etc. I have then created some users and they can/can not access folders according to the web.config in the various folders. This works OK.
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))
etc
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 ApplicationDbContext())) 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:
An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code
Additional information: The entity type IdentityUser is not part of the model for the current context.
I cannot understand the message - what is the 'model' it refers to and what the 'current context'? It's a Web Forms only project.
Much appreciated if someone could help identify the problem.
Tuesday, January 5, 2016 12:44 PM
Answers
-
User614698185 posted
Hi davidxt,
To start using ASP.NET Identity I advice the following steps:
1.Generate a code model from your existing database.
2.On the tables you'd like to use for your Identity implementation inherit from the appropriate classes.
3.Create a migration and apply the changes to your database.
To enable identity with the files you generated from your database:
1.Inherit the generated context from IdentityDbContext instead of DbContext.
2.Inherit your user model from IdentityUser.
Based on your situation there are more models of your own you need to inherit from one of the identity classes or you have to add (using code first migrations). See https://msdn.microsoft.com/en-us/library/dn613255%28v=vs.108%29.aspx for more information about the IdentityDbContext.
Best Regards,
Candice Zhou
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 6, 2016 2:24 AM -
User614698185 posted
Hi David,
This method requires the needed amount of maintenance when updating your database either with code first migrations or from your database itself. It's not completely impossible but you might have to regenerate your code first model several times or turn you can turn off the model compatibility check and apply the updates manually to your model.
Best Regards,
Candice Zhou
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, January 8, 2016 9:14 AM
All replies
-
User614698185 posted
Hi davidxt,
To start using ASP.NET Identity I advice the following steps:
1.Generate a code model from your existing database.
2.On the tables you'd like to use for your Identity implementation inherit from the appropriate classes.
3.Create a migration and apply the changes to your database.
To enable identity with the files you generated from your database:
1.Inherit the generated context from IdentityDbContext instead of DbContext.
2.Inherit your user model from IdentityUser.
Based on your situation there are more models of your own you need to inherit from one of the identity classes or you have to add (using code first migrations). See https://msdn.microsoft.com/en-us/library/dn613255%28v=vs.108%29.aspx for more information about the IdentityDbContext.
Best Regards,
Candice Zhou
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 6, 2016 2:24 AM -
User-626985523 posted
Hi Candice
thanks for your response, it helped me understand better how to begin.
I went into Project>Add new item >ADO.NET Entity Data model and created a model from the database that .net had already created in my external SQL db when the template project first was run and I added a User via the Resister page
I did not need to add any inherits to anything (step 2), nor did I want to use Code First so (although it taught me about Migrating) did I need to use Step 3
I did not understand your last two steps unfortunately but with the above in place and without further changes I can add a user to a role with
UserManager.AddToRole(user.Id, <value from control>) without an error.
kind regards
David
Wednesday, January 6, 2016 7:29 PM -
User614698185 posted
Hi David,
This method requires the needed amount of maintenance when updating your database either with code first migrations or from your database itself. It's not completely impossible but you might have to regenerate your code first model several times or turn you can turn off the model compatibility check and apply the updates manually to your model.
Best Regards,
Candice Zhou
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, January 8, 2016 9:14 AM