user1027620
user1027620

Reputation: 2785

ASP.NET Membership and EF

I have a table called Websites that could have many users. (Many-2-Many relationship between Websites and Users). However, I am using ASP.NET Built-in Membership API and Entity Framework to handle data.

What I would like to do, is to add a User to a Website object using EF as such: website.Users.Add(MembershipUser object here). Simple as that, if this wasn't the Membership API it would've been much easier. But When creating a new user i'd like to assign access to specific websites from the websites table from a checkbox list.

How can I do this?

Thanks

Upvotes: 0

Views: 193

Answers (2)

Win
Win

Reputation: 62301

You question is a lot more complex, and you might not be able to get all answers in a single question. I'll try my best.

Here is my approach -

1) Use new ASP.Net Universal Provider

2) Implement Custom Membership Provider and override

public override bool ValidateUser(string username, string password)

So that you can validate user against Users table as well as Websites table.

3) Create registration yourself instead of using CreateUserWizard. For example, using regular textboxes.

MembershipCreateStatus status;

MembershipUser membershipUser = System.Web.Security.Membership.CreateUser(
UsernameTextBox.Text, PasswordTextBox.Text, EmailTextBox.Text, 
PasswordQuestioTextBox.Text, PasswordAnswerTextBox.Text, true, out status);

// Then assign user to website based on the selected checkboxes

Database Relationship

enter image description here

Upvotes: 1

Dennis Traub
Dennis Traub

Reputation: 51684

You can include the aspnet_Users table in your mapping and add the respective constraints in the websites' table. EF will then provide a class representing the users and a collection of these as a navigation property on Websites.

Upvotes: 1

Related Questions