MembershipUser -> User

Apr 21, 2012 at 7:24 PM
Edited Apr 21, 2012 at 7:36 PM

CodeFirstMemberShipProvider and WebSecurity classes provides methods which are returning instance of MembershipUser, but in my DbContext I'm storing User class instances. Is there easy way to converting from one to another or I should map properties manually? Or I even totally missing point somewhere?

Apr 22, 2012 at 3:25 AM

Yes the membeship interface is built like this, so no straightforward way. Look at how the Register function was wrapped in WebSecurity, how FirstName and LastName were added. You can extend all the membership functions like this.

Or you can just use this provider for Login and Register purposes and build your own methods for FindUser GetAllUsers GetUserRole etc. Using standard EF means, but be carefull what you return, you should never return full user with password hash and the rest of sensitive data, you should return some object that is a subset of the User, or at the very least an annonymous type.

Apr 22, 2012 at 12:16 PM

Never been working with ASP.NET Membership before, but as far as I understood, information like password, creation date, etc is storing separately from database generated based on DbContext? If it is so, there is no very reason to have this fields in User entity, isn't it?

Apr 22, 2012 at 12:37 PM

Yes in the original implementation it is, but not in mine, its only User and Role entities. This way is much simpler and more maintanable.

If you wish to seperate properties across Membership and User you will have to do allot of rewrite, in that case i would recommend you use Universal Providers.

This project is a good starting point if you want to customize and evolve membership and the rest of data in just one database. With EF migrations its pretty straight forward now. If your requirement is not a seperate database for membership and if you will not use multiple applications with one membership, this provider will be perfect for you.