MVC 3 Basic Custom Membership

2019-06-06 17:49发布

问题:

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

So, let's say that:

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)

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

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

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

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

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

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

Step 8, I Build Solution

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

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.


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

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

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

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.

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

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();

Step 15, Build Solution, Save All, Run

回答1:

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

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.)

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.

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.

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.