Create Users for asp.net identity 2.x from winforms

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
nhill383
Posts: 13
Joined: Sun 26 Apr 2015 21:43

Create Users for asp.net identity 2.x from winforms

Post by nhill383 » Tue 26 May 2015 10:28

Hi,

I am exploring MVC Identity 2.x. I have followed the sample in
Using Entity Framework Implementation of ASP.NET Identity 2 for PostgreSQL https://www.devart.com/dotconnect/postg ... ntity.html
and all seems to work fine.

I would like to use the same credentials within an accompanying winforms app. I have successfully implemented login and check the credentials using the following code

Code: Select all

 Public Function VerifyUserNamePassword(ByVal userName As String, ByVal password As String) As Boolean

        Dim usermanager = New UserManager(Of IdentityUser)(New UserStore(Of IdentityUser)(New IdentityDbContext("DefaultConnectionTwo")))
        Return usermanager.Find(userName, password) IsNot Nothing

    End Function
I would like to be able to create users from within winforms as well. the following code

Code: Select all

 Dim usermanager = New UserManager(Of IdentityUser)(New UserStore(Of IdentityUser)(New IdentityDbContext("DefaultConnectionTwo")))

        Dim newuser As New ApplicationUser
        newuser.Email = TextBox3.Text
        newuser.UserName = TextBox3.Text

        MessageBox.Show(usermanager.Create(newuser, TextBox4.Text).ToString)
gives the error System.InvalidOperationException, Message=Mapping and metadata information could not be found for EntityType 'WinformsIdentity.ApplicationUser'.

I have imported the models from the mvc project, but not the controllers. I suspect this is the issue, but not quite sure how to achieve it.
Is is possible to do what i am trying to do, if so , how do i modify my project to be able to add users.
I will post a sample project

many thanks
Nick

Shalex
Site Admin
Posts: 8247
Joined: Thu 14 Aug 2008 12:44

Re: Create Users for asp.net identity 2.x from winforms

Post by Shalex » Fri 29 May 2015 16:30

Please use

Code: Select all

Dim usermanager = New UserManager(Of ApplicationUser)(New UserStore(Of ApplicationUser)(New ApplicationDbContext()))
instead of

Code: Select all

Dim usermanager = New UserManager(Of IdentityUser)(New UserStore(Of IdentityUser)(New IdentityDbContext("DefaultConnectionTwo")))
in both VerifyUserNamePassword and Create_Click methods.

This should fix the issue you have encountered.

nhill383
Posts: 13
Joined: Sun 26 Apr 2015 21:43

Re: Create Users for asp.net identity 2.x from winforms

Post by nhill383 » Sun 31 May 2015 19:59

That works perfectly, thank you.

for anyone else who come across this issue, i had encountered the following error
"There is already an object named 'AspNetRoles' in the database."

The following article describes this much better than i could and the solution therein works fine for me.
http://stackoverflow.com/a/24170172/2808914

In addition to the changes described above, i also changed the shared constructor for the ApplicationDbContext class as described in the article above
As i understand it, this is necessary as the winforms application does not "own" the database migration, this was owned in my case by an asp.net mvc project. In essence one project needs to be the primary and maintain the code-first database migrations, any other projects that try to use the same identity database need to be slaves and not try to do any migrations

Code: Select all

  Shared Sub New()
        Database.SetInitializer(New MigrateDatabaseToLatestVersion(Of ApplicationDbContext, Configuration)())
    End Sub
to

Code: Select all

Shared Sub New()
        Database.SetInitializer(Of ApplicationDbContext)(Nothing)
    End Sub
I am happy for the issue to be marked a closed, thank you for your assistance

Post Reply