简体   繁体   English

EF的ASP.NET成员资格提供者

[英]ASP.NET membership provider with EF

I have been using ASP.NET membership provider the way it comes and it s been just fine serving my basic purposes. 我一直在使用ASP.NET成员资格提供程序,它一直很好地服务于我的基本目的。 One thing I realize is that, it installs bunch of Stored Procs etc to the database. 我意识到的一件事是,它将一堆Stored Procs等安装到数据库中。

Is there a EF implementation of ASP.NET membership ? 是否有ASP.NET成员资格的EF实现? or will there be? 还是会有?

I have an up coming project which i will need to extend the functionality of the Membership providers with roles and authorization via some GUI. 我有一个即将到来的项目,我需要通过一些GUI扩展成员资格提供者的功能和角色和授权。

Moreover, everytime I use this, I have two connection string in my web.config one for DbContext one for Application services particularly for Membership provider. 此外,每次我使用它时,我在web.config中有两个连接字符串,一个用于DbContext,一个用于Application服务,特别是对于Membership provider。 Why cant i have just one? 为什么我不能只有一个?

The universal providers are what you are looking for: http://nuget.org/packages/Microsoft.AspNet.Providers.Core 您正在寻找通用提供商: http//nuget.org/packages/Microsoft.AspNet.Providers.Core

These are implemented using EF Code First 5 internally, we will be making the internal DBContexts public once we make sure everything works smoothly. 这些是在内部使用EF Code First 5实现的,一旦我们确保一切顺利进行,我们将公开内部DBContexts。

For ASP.NET membership provider and Roles implementation in Entity Framework, need to import all views of ASPNETDB (Membership database) in EDMX file. 对于ASP.NET成员资格提供程序和Entity Framework中的Roles实现,需要在EDMX文件中导入ASPNETDB(成员资格数据库)的所有视图。 eg vw_aspnet_MembershipUsers, vw_aspnet_Roles, vw_aspnet_UsersInRoles, vw_aspnet_Users, etc... 例如vw_aspnet_MembershipUsers,vw_aspnet_Roles,vw_aspnet_UsersInRoles,vw_aspnet_Users等...

Then Membership will be run via EF. 然后会员资格将通过EF运行。 So, You can provide GUI using below functions. 因此,您可以使用以下功能提供GUI。

here is controller code. 这是控制器代码。 eg 例如

  // GET: /Membership/Edit/5

    public ActionResult Edit(Guid id)
    {
        var recordToEdit = (from r in _db.vw_aspnet_Users where r.UserId == id select r).First();
        return View(recordToEdit);
    }

    public ActionResult Index()
    {
        return View(_db.vw_aspnet_MembershipUsers.ToList());
    }

Now, How to merge the ASPNETDB to existing Database, to make only one connection string in web.config. 现在,如何将ASPNETDB合并到现有数据库,在web.config中只创建一个连接字符串。 (Your question: Why cant i have just one?) (你的问题:为什么我只有一个?)

That is also possible using below steps. 使用以下步骤也是可能的。

ASPNETDB.MDF is membership provider database and is used for storing and retrieving membership data from database and here we are going to see how to create membership provider database . ASPNETDB.MDF是成员资格提供者数据库,用于存储和检索数据库中的成员资格数据,在这里我们将看到如何创建成员资格提供者数据库。 The command used to create Aspnetdb.mdf is ASPNET_RegSQL.EXE 用于创建Aspnetdb.mdf的命令是ASPNET_RegSQL.EXE

1.Start->Programs->Microsoft visual studio 2005->visual studio tools->Visual Studio 2005 command prompt. 1.Start-> Programs-> Microsoft visual studio 2005-> visual studio tools-> Visual Studio 2005命令提示符。 Type ASPNET_RegSQL.EXE in the visual Studio 2005 command prompt 在Visual Studio 2005命令提示符下键入ASPNET_RegSQL.EXE

  1. A wizard with the heading ” Welcome To The Asp.Net Sql Server Wizard ” will be displayed. 将显示标题为“欢迎使用Asp.Net Sql Server向导”的向导。 Here need to click Next 这里需要单击“下一步”

  2. Next a wizard with ” Select Setup Option ” will displayed. 接下来将显示带有“选择安装选项”的向导。 Now we need to select setup option “Configure sql server for application purpose is default”. 现在我们需要选择安装选项“为应用程序配置sql server是默认的”。 Select which one you wants and next. 选择你想要的和下一个。

  3. A window with ” Select Sql Server Database ” Will be shown Now we need to select our sql server database. 将显示一个带有“选择Sql Server数据库”的窗口现在我们需要选择我们的sql server数据库。 Here needs to set server, authentication type and database. 这里需要设置服务器,身份验证类型和数据库。 If select the default name”aspnetDb.mdf” will be selected. 如果选择,将选择默认名称“aspnetDb.mdf”。 If you wants to modify existing database select that database. 如果要修改现有数据库,请选择该数据库。

5.Now A confirm will be displayed with heading “Confirm your Settings”. 5.现在将显示标题为“确认您的设置”的确认。 Now check servername and database name and click next. 现在检查servername和数据库名称,然后单击下一步。

  1. A window with ” The database Has Been Created Or Modified ” Will displayed. 将显示“已创建或修改数据库”的窗口。 Now click Finish 现在单击Finish

Be careful in above steps you need to select your existing database, Some tables (11+), views, stored procedures of Membership and Roles will be added to your existing database.... 在上面的步骤中要小心,您需要选择现有的数据库,一些表(11 +),视图,成员和角色的存储过程将添加到您现有的数据库....

Enjoy it....Thank you... 享受它....谢谢......

There are gui membership management systems for MVC3 you can take a look at or add to your project. 有MVI3的gui会员管理系统,您可以查看或添加到您的项目。

You can play around with them and see how they work. 你可以和他们一起玩,看看他们是如何工作的。 It's simple once you know where to look and you can create your own. 一旦你知道在哪里看就很简单,你可以创建自己的。

The Membership Class and MembershipUser Class holds all the user info. Membership ClassMembershipUser Class包含所有用户信息。 For example to un-approve a user you can do this: 例如,要取消批准用户,您可以执行以下操作:

MembershipUser user = Membership.GetUser("userName");
user.IsApproved = false;
Membership.UpdateUser(user);

And if you want to delete a user, you can use Membership.DeleteUser("userName"); 如果要删除用户,可以使用Membership.DeleteUser("userName"); .

The Roles Class has all the role associated info. 角色类具有所有角色相关信息。 Like Roles.GetUsersInRole("roleName") will return a list of all the users in the role roleName . Roles.GetUsersInRole("roleName")将返回角色roleName中所有用户的列表。 Roles.CreateRole("roleName"); will create a role, and Roles.DeleteRole("roleName"); 将创建一个角色,和Roles.DeleteRole("roleName"); will delete a role. 将删除一个角色。

If you need more than just the default, you can look at Implementing a Membership Provider . 如果您需要的不仅仅是默认值,您可以查看实施成员资格提供程序

As far as having two databases, this is not necessary. 至于拥有两个数据库,这不是必需的。 You can add all the ASP.NET tables, stored procedures, etc. to your database by using the Aspnet_regsql tool. 您可以使用Aspnet_regsql工具将所有ASP.NET表,存储过程等添加到数据库中。 Take a look at Installing the Database using Aspnet_regsql.exe . 看一下使用Aspnet_regsql.exe安装数据库 This will allow you to only have one database, so you can remove the asp.net connection string in your Web.Config and then change the AspNetSqlMembershipProvider AspNetSqlProfileProvider AspNetSqlRoleProvider and maybe others, to use the connection string to your main database. 这将允许您只有一个数据库,因此您可以删除Web.Config中的asp.net连接字符串,然后更改AspNetSqlMembershipProvider AspNetSqlProfileProvider AspNetSqlRoleProvider以及其他人,以使用连接字符串到主数据库。

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

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