Add a default ASP. NET MVC 4 project with an account controller that uses forms authentication, and automatically generate several tables if you use Codefirst.
< connectionStrings > < name= "DefaultConnection" connectionString= "Data source=.;i Nitial catalog=websecurity; Persist Security info=true; User Id=sa; password=1q2w3e4r " providerName=" System.Data.SqlClient "/> </connectionStrings>
Public classEntitycontext:dbcontext { PublicEntitycontext ():Base("defaultconnection") { } PublicDbset<userprofile> Userprofiles {Get;Set; } //protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)//{ //base. Onmodelcreating (ModelBuilder); //modelbuilder.entity<userprofile> (). ToTable (""); //}}
Database generates 5 tables:
UserProfile, Webpages_membership, Webpages_oauthmembership, Webpages_roles, Webpages_usersinroles
Registered users
[ HttpPost] [allowanonymous] [Validateantiforgerytoken] PublicActionResult Register (Registermodel model) {if(modelstate.isvalid) {//try to register a user Try { //Create userWebsecurity.createuserandaccount (model. UserName, model. Password); //Create a role if(! Roles.roleexists ("Superadmin") {roles.createrole ("Superadmin"); } if(! Roles.roleexists ("Admin") {roles.createrole ("Admin"); } if(! Roles.roleexists ("User") {roles.createrole ("User"); } //add a user to a roleRoles.addusertorole (model. UserName,"User"); varID =Websecurity.getuserid (model. UserName); Websecurity.login (model. UserName, model. Password); returnRedirecttoaction ("Index","Home"); } Catch(membershipcreateuserexception e) {modelstate.addmodelerror ("", Errorcodetostring (E.statuscode)); } } //if something goes wrong when we go to this step, re-display the form returnView (model); }
So, in addition to the third table has data, the Oauthmembership table has three fields provider, Provideruserid, UserId, should be related to the configuration of authentication mode.
Simple usage of websecurity