Simple usage of websecurity

Source: Internet
Author: User
Tags connectionstrings

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.