2

I'm using ASP.NET MVC 3 to built my site (former PHP programmer), using Entity Framework and I wish to integrate the built-in membership support. Although I've read lot of questions and tutorials I couldn't figure out wish approach should I use when using MVC and EF.

I read about the MembershipProvider but it seems like a ASP.NET web-forms related...

Which technique should I use in order to integrate membership (with many properties, even different sign on ways: FB, twitter etc.) to my site and be able to integrate it with my currently Entity framework data. Note: I've used code-first approach but I don't mind to rewrite it since I didn't got anything special yet.

tereško
  • 58,060
  • 25
  • 98
  • 150
OzB
  • 2,140
  • 1
  • 22
  • 38
  • 1
    Did you read this? http://msdn.microsoft.com/en-us/library/ff398049(VS.98).aspx – Marc Jul 03 '12 at 02:18
  • Yeah, but got lost because this is a different database, and I can't figure out how to integrate the membership and roles to my current Entity Framework data – OzB Jul 03 '12 at 02:21

3 Answers3

1

It looks like you will need to implement your own membership provider. Not very easy, but doable. See the MembershipProvider class. This implementation might help you as well.

dan radu
  • 2,772
  • 4
  • 18
  • 23
1

How do you mean to your current EF data? The two generally can remain separate. Auth is a security concern, your EF classes are a business domain concern, so the two generally can remain separate and asp.net membership will run in its own separate database.

The question now is iif you want all these tables in the same database (which many do) in that case use Ambers suggestion below with aspnet_regsql.exe although you'll need to update the connect string for the membership and role provider. In this way using EF code first you can use your existing EF connection string (you can't do this however with database first and the edmx connect string)

To configure the db connection string for membership: http://msdn.microsoft.com/en-us/library/6e9y4s5t.aspx

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • Please explain me why keep them separate, why have the users email in this database and his other info in another database, does this make sense to you? – Shimmy Weitzhandler Nov 02 '12 at 02:04
  • @Shimmy That depends : ) If you need the email address you have several options. 1. Create a read-only entity (Instead of DbSet use DbQuery http://stackoverflow.com/questions/10437058/how-to-make-entity-framework-data-context-readonly) to query only the membership table for that field. 2. ALSO store it in your application when the user registers. 3. Implement membership in your database and use a single database for everything. You will still need to query the email address. Note in #1 and #3 if the email address IS the login then you simply query HttpContext.Current.User.Identity.Name – Adam Tuliper Nov 06 '12 at 06:01
  • @Shimmy #4, create your own custom membership provider. Note asp.net 4.5 has the new SimpleMembership class and in addition the WebMatrix binaries support logging in via oauth (and thus can be used in mvc and thus can get the email address) – Adam Tuliper Nov 06 '12 at 06:02
  • I ended up up using [SimpleMembershipProvider](http://msdn.microsoft.com/en-us/library/webmatrix.webdata.simplemembershipprovider.aspx). A great extension! Thanks a lot for your info. – Shimmy Weitzhandler Nov 06 '12 at 06:11
0

You can use standart MembershipProvider in ASP .NET MVC 3 too. It doesn't use EF connection, it works with database in its own way.

You should prepare database with aspnet_regsql.exe, and implement your logic - authentification, creating new users, adding new roles for them, changing password and so on. In general, the best way is to study Test Internet Application in VS.

If the functions of standart MembershipProvider isn't enough for you, you have to write your own implementation.

Sir Hally
  • 2,318
  • 3
  • 31
  • 48