MVC 3基本自定义成员资格 [英] MVC 3 Basic Custom Membership

查看:118
本文介绍了MVC 3基本自定义成员资格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要尝试解决这个会员系统1,一次一小步......

I am going to try and tackle this Membership system 1 little step at a time...

所以,让我们说:

步骤1,我创建一个SQL数据库,该数据库中我有一个用户表,非常简单,看起来像这样:

Step 1, I create an SQL database and in that database I have a Users table, very basic, that looks like this:

Users
UserID int, identity, primary key
UserName nvarchar(25)
UserPassword nvarchar (25)
UserEmail nvarchar (75)

步骤2,创建一个新的ASP.NET MVC3 Web应用程序

Step 2, I create a new ASP.NET MVC3 Web Application

步骤3,我选择用剃刀视图引擎在互联网应用程序模板,并检查使用HTML 5语义标记

Step 3, I select the Internet Application template with the Razor view engine and check Use HTML 5 semantic markup

第四步,我增加一个ADO.NET实体数据模型

Step 4, I add an ADO.NET Entity Data Model

步骤5,在实体数据模型向导,我选择从数据库中生成

Step 5, In the Entity Data Model Wizard, I choose to Generate from database

第六步,我选择我的数据连接,并选择保存Web.Config中的实体连接设置

Step 6, I select my data connection and select to Save entity connection settings in Web.Config

第7步,在实体数据模型向导==>选择您的数据库对象,我把表检查,并留下或复数形式产生的单数对象名称默认的检查和在模型中包含外键列然后单击Finish

Step 7, In the Entity Data Model Wizard ==> Choose Your Database Objects, I put a check in Tables and leave the default checks in "Pluralize or singularize generated object names" and "Include foreign key columns in the model" and click Finish

步骤8,我生成解决方案

Step 8, I Build Solution

第9步,我右键单击该.edmx文件并选择添加code代项目......

Step 9, I right click in the .edmx file and choose to "Add Code Generation Item..."

第10步,我添加了一个新的ADO.NET的DbContext发生器(这则创建所有的表款)

Step 10, I add a new ADO.NET DbContext Generator (This then creates all of the table models)

好了,这是我不知道如何去任何进一步使用内置的账户/会员系统,我的用户表。对于现在,对于这个特定的项目,无论如何,我不需要角色和什么,而不是,只是[授权]功能...

OK, so this is where I don't know how to go any further with using the built-in Account/Membership system with my Users table. For right now, for this particular project anyway, I don't need Roles and what-not, just the [authorize] functionality...

那么,究竟是什么,逐字遵守,我是否需要为了当用户来到网站,并在寄存器或日志,对于使用我的用户表的申请呢?最终使得当用户不登录时,[授权]装饰会为我的用户群使用。

So, what exactly, verbatim compliance, do I need to do in order for when a user comes to the website and registers or logs in, for the application to use my Users table? Ultimately so that when a user does log in, the [authorize] decoration will work for my user base.

编辑:感谢olivehour ...以下修改,补充真,使这项工作...

Thanks to olivehour ... The following changes, additions really, to make this work...

在第10步:(侧面说明:从用户表中删除的userPassword,你不会需要它)

After Step 10: (side note: remove the UserPassword from your Users table, you won't need it)

第11步,运行aspnet_regsql.exe的工具ASP.NET的表添加到数据库中。

Step 11, Run the aspnet_regsql.exe tool to add ASP.NET's tables to your database.

第12步,打开你的Web.config文件,从你的EntityFramework的connectionString复制只是数据源的信息,然后粘贴和替换ApplicationServicesConnectionString的数据源这一说法的的EntityFramework的。

Step 12, Open up your Web.config file, copy just the "data source" information from your EntityFramework connectionString, then paste and replace the "ApplicationServices" connectionString "data source" with that of the EntityFramework's.

步骤13,在解决方案资源管理器中,右键单击控制器上的目录,并添加控制器。在这一点上继续前进,添加UserController中

Step 13, In the Solution Explorer, right click on the Controller directory and Add Controller. At this point go ahead and add your UserController

步骤14,在的AccountController,在[HttpPost]寄存器的操作方法,在如果(createStatus == MembershipCreateStatus.Success)语句里面,添加以下内容:

Step 14, In the AccountController, in the [HttpPost] Register action method, inside of the "if (createStatus == MembershipCreateStatus.Success)" statement, add the following:

TheNameOfYourEntities db = new TheNameOfYourEntities();
User user = new TheNameOfYourProject.User();
user.UserName = model.UserName;
user.UserEmail = model.Email;
db.Users.Add(user);
db.SaveChanges();

步骤15,生成解决方案,全部保存,运行

Step 15, Build Solution, Save All, Run

推荐答案

我们保持内置成员提供从我们的应用程序的用户表分开。

We keep the built-in membership provider separate from our application users table.

我建议使用内置成员提供程序来处理用户身份验证。有迹象表明,需要你做出一些决定某些设置。例如,将用户名是什么?你要允许电子邮件地址格式为?如果是这样,你应该设置requiresUniqueEmail为true web.config中的供应商元素。 (我们使用户的电子邮件地址他们的用户名。这简化了很多东西。)

I suggest using the built-in membership provider to handle user authentication. There are some settings that require you to make some decisions. For example, what will the username be? Do you want to allow email addresses as usernames? If so, you should set the requiresUniqueEmail to true on the provider element in web.config. (We make the user's email address their username. This simplifies things a lot.)

至于您在使用EF创建自定义的用户表,不要使用用于登录即可。用它来存储用户应用程序特定的信息。但是,给表中的行相同的主键值作为成员资格提供数据库的用户名。

As for your custom Users table that you created using EF, don't use that for login. Use it to store application-specific information about your users. But, give rows in the table the same primary key value as the username in the membership provider db.

所以,如果用户使用的电子邮件地址注册olivehour@stackoverflow.com,你会先做 Membership.CreateUser 将它们添加到供应商数据库,并在同行动,将行添加到您的用户表olivehour@stackoverflow.com的主键。

So, if a user registers with email address olivehour@stackoverflow.com, you would first do Membership.CreateUser to add them to the provider db, and in the same action, add a row to your users table with a primary key of olivehour@stackoverflow.com.

这种方式,你从来没有在数据库中存储的任何密码加密值......你把它外包给成员提供。当用户登录, FormsAuthentication 会写一个cookie保持登录状态。在控制器的行动,你可以用code User.Identity.Name 的用户名。使用该值作为参数,从您的自定义应用程序特定的用户表中选择行。

This way you never have to store any password encryption values in your database... you outsource it to the membership provider. When a user signs in, FormsAuthentication will write a cookie to maintain the login status. In a controller action, you can get the username with the code User.Identity.Name. Use that value as a parameter to select rows from your custom application-specific users table.

这篇关于MVC 3基本自定义成员资格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆