1

I have a PHP/MySQL-system that I'm porting to ASP.NET MVC5 with Azure SQL. I have a problem with grasping the concept of the new Identity solution that Microsoft has introduced. Becuase the database structure exists from the previous system, I have transfered the old MySQL database to Azure SQL. So far so good. There I have a user table with all user related data, such as username, email password etc. Becuase I wanted to continue using this table, I created the extra database fields that exists in the Identity user table. I have also created custom UserManager and other classes (MyUser, MyClaim etc.) so I am able to log in. Everything works.

Since I have a database first approach, not a code first approach, I wanted to use the User class to interact with the user table. But my UserManager returns the MyUser class (that inherits from IdentityUser), not the User class in my model. Even my dbcontext returns the MyUser class when I ask for the all the users in the User table.

Do I really need to manually update the MyUser class to replicate the User table, so if I do a change in the User table and updates my model, I have to manually update the MyUser class because the system can't/woun't use the models user class?

AKG
  • 337
  • 3
  • 18
  • I would suggest implementing [custom membership](http://logcorner.wordpress.com/2013/08/29/how-to-configure-custom-membership-and-role-provider-using-asp-net-mvc4/). – Andrei V Nov 11 '14 at 07:45
  • Thank you, I would look into that! – AKG Nov 11 '14 at 07:56
  • @AndreiV I highly discourage in using MembershipProvider in any new developments. Identity framework is far better than depricated approach you talk about. – trailmax Nov 11 '14 at 10:27
  • Sorry, I'm not clear about your question. What is the difference between `User` and `MyUser` classes? Are they in different tables? Can you show the code for them? Why do you have 2 of them? – trailmax Nov 11 '14 at 10:30
  • @trailmax I don't have an opposing argument for your statement. However, I can't help but notice the word _new_ in what you wrote and the context of the question. They don't really go hand in hand. I hope you can give some pointers on how the identity framework could be used in this scenario, as I have a similar issue. – Andrei V Nov 11 '14 at 10:34
  • @AndreiV from the question I understand the application is currently ported from PHP to ASP.Net MVC. This is not 1-to-1 transfer and involves basically a full rewrite into the new language. In that sense this is a green-field development relating to MVC, hence "new". – trailmax Nov 11 '14 at 10:46
  • @trailmax the User class id automatic generated from the database (database first apporach). The MyUser class is a name i used following a tutorial on customizing the userstore and usermanager. The MyUser inherits the IdentityUser. Since the User class don't, I'm unable to retreive it from the UserManager. – AKG Nov 11 '14 at 17:22
  • Ah, I see. Have a look on this question: http://stackoverflow.com/q/19940014/809357 – trailmax Nov 11 '14 at 19:35

1 Answers1

1

IdentityUser represents a default EntityFramework IUser implementation. Since you have your own existing schema, you're going to have to hand roll some classes to implement ASP.NET Identity to use your own tables.

In order to use your User class with ASP.NET Identity, you'll need to make sure it inherits from Microsoft.AspNet.Identity.IUser. After implementing IUser, update all references to MyUser with a reference to your User class. Note: You'll probably want to update your User class to something like MyAppNameUser for clarity.

After doing the above, you'll probably need to implement IUserStore so that the UserManager class can interact with your database schema.

You may want to take a look at https://github.com/raquelsa/AspNet.Identity.MySQL to get some ideas on implementing some of the ASP.NET Identity interfaces.

cpp
  • 81
  • 3
  • My User class (that equals my User table) is auto-generated and belongs to the .edmx file. Is you suggestion that I manually change that class to inherit from the IUser interface? So if i update the model from the database, I have to manually change it again? – AKG Nov 13 '14 at 20:03
  • You can create a User partial class and implement IUser there. You won't want to modify the auto-generated partial classes. Here's an example: http://stackoverflow.com/questions/2606461/problem-with-interface-implementation-in-partial-classes – cpp Nov 13 '14 at 21:08