简体   繁体   English

ASP.NET成员资格和EF

[英]ASP.NET Membership and EF

I have a table called Websites that could have many users. 我有一个名为Websites的表可能有很多用户。 (Many-2-Many relationship between Websites and Users). (网站和用户之间的许多关系)。 However, I am using ASP.NET Built-in Membership API and Entity Framework to handle data. 但是,我使用ASP.NET内置成员资格API和实体框架来处理数据。

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). 我想做的是使用EF将User添加到Website对象:website.Users.Add(MembershipUser object here)。 Simple as that, if this wasn't the Membership API it would've been much easier. 就这么简单,如果这不是会员API,那将会容易得多。 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 谢谢

You can include the aspnet_Users table in your mapping and add the respective constraints in the websites' table. 您可以在映射中包含aspnet_Users表,并在网站表中添加相应的约束。 EF will then provide a class representing the users and a collection of these as a navigation property on Websites . 然后,EF将在Websites上提供一个表示用户的类及其集合作为导航属性。

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 1)使用新的ASP.Net Universal Provider

2) Implement Custom Membership Provider and override 2)实现自定义成员资格提供程序并覆盖

public override bool ValidateUser(string username, string password)

So that you can validate user against Users table as well as Websites table. 这样您就可以针对Users表和Websites表验证用户。

3) Create registration yourself instead of using CreateUserWizard. 3)自己创建注册,而不是使用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 数据库关系

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM