How to modify the data type of the Microsoft.AspNet.Identity.EntityFramework.IdentityUser Id field in the ASP. NET Identity Series

Source: Internet
Author: User
Tags mail code

This blog we will learn how to change the ID field of the Aspnetusers table from nvarchar (128) to int and child growth

1, why to modify

If you have run the ASP. NET MVC sample project, you will find that the ID of the Aspnetusers table is the nvarchar (128) type, the value is a GUID, and it is undeniable that using the GUID to master the primary Key association will increase the data security (personal view), but it is very detrimental to the query , readability is not enough, so we try to change to the int type.

First look at the modified effect:

2. Pre-modification analysis

View database structure We know that the table to be modified has such four tables that they relate to userid,roleid. The concrete structure does not do the analysis here.

1 SELECT *  from dbo. Aspnetusers2SELECT* from dbo. Aspnetroles3SELECT* from dbo. Aspnetuserroles4SELECT* from dbo. Aspnetuserlogins
3. How to modify the Code implementation function

If you study the ASP. NET Identity, you will find that it is very extensible.

We create a new mvc4,5 project and open Models-->identitymodels.cs. View to ApplicationUser class (and user-related classes) inherited IdentityUser.cs ( At Microsoft.Asp.Net.Identity.EntityFramework.dll) The following source code is compiled:

1 namespaceMicrosoft.AspNet.Identity.EntityFramework2 {3     usingMicrosoft.AspNet.Identity;4     usingSystem;5     6      Public classidentityuser:identityuser<string, Identityuserlogin, Identityuserrole, Identityuserclaim>, Iuser, iuser<string>7     {8          PublicIdentityuser ()9         {Ten              This. Id =Guid.NewGuid (). ToString (); One         } A          -          PublicIdentityuser (stringUserName): This() -         { the              This. UserName =UserName; -         } -     } -}

Observing IdentityUser.cs It had inherited identityuser<string, Identityuserlogin, Identityuserrole, Identityuserclaim>, IUser, Iuser<string> This generic, so we also need to have the corresponding int type of Identityuserlogin, Identityuserrole, Identityuserclaim generic class.

As shown below:

1      Public classintrole:identityrole<int, intuserrole>2     {3          PublicIntrole ()4         {5 6         }7          PublicIntrole (stringName): This() {Name =name;}8     }9      Public classintuserrole:identityuserrole<int> { }Ten      Public classintuserclaim:identityuserclaim<int> {} One      Public classintuserlogin:identityuserlogin<int> { } A  -      Public classIntusercontext:identitydbcontext<applicationuser, Introle,int, Intuserlogin, Intuserrole, intuserclaim> -     { the          PublicIntusercontext () -:Base("defaultconnection") -         { -  +         } -}

Then we modify the Application.cs

1      Public classapplicationuser:identityuser<int, Intuserlogin, Intuserrole, intuserclaim>2     {3          PublicApplicationUser () {}4          Public AsyncTask<claimsidentity> Generateuseridentityasync (Usermanager<applicationuser,int>manager)5         {6             //Note that the AuthenticationType must match the corresponding items defined in the Cookieauthenticationoptions.authenticationtype7             varUseridentity =awaitManager. Createidentityasync ( This, Defaultauthenticationtypes.applicationcookie);8             //Add custom user claims here9             returnuseridentity;Ten         } One          PublicApplicationUser (stringName): This() {UserName =name;} A}

As we have made changes to the Application.cs, we also need to review the Identityfig.cs and make corresponding changes. The modified code is as follows:

Public   class applicationusermanager:usermanager<applicationuser, int>    {        Public   Applicationusermanager (iuserstore<applicationuser, int> Store) :Base(store) {} Public StaticApplicationusermanager Create (identityfactoryoptions<applicationusermanager>options, Iowincontext context) {             var manager = new applicationusermanager (new intuserstore (context. Get<applicationdbcontext>()));

//Configure authentication logic for user names Manager. Uservalidator = new uservalidator<applicationuser, int> (manager) {allowonlyalphanumericusernames=false, Requireuniqueemail=true, }; //Configure authentication logic for passwordsManager. Passwordvalidator =NewPasswordvalidator {requiredlength=6, Requirenonletterordigit=false, Requiredigit=false, Requirelowercase=false, Requireuppercase=false, }; //Configure user Lock default valuesManager. Userlockoutenabledbydefault =true; Manager. Defaultaccountlockouttimespan= Timespan.fromminutes (5); Manager. Maxfailedaccessattemptsbeforelockout=5; //registers a two-factor authentication provider. This application uses mobile phones and e-mail as a step to receive code to authenticate users//you can write your own provider and insert it here. Manager. Registertwofactorprovider ("Phone Code",NewPhonenumbertokenprovider<applicationuser,int>{Messageformat="your security code is {0}" }); Manager. Registertwofactorprovider ("e-Mail code",NewEmailtokenprovider<applicationuser,int>{Subject="Security Code", BodyFormat="your security code is {0}" }); Manager. Emailservice=NewEmailservice (); Manager. SMSService=NewSMSService (); varDataprotectionprovider =options. Dataprotectionprovider; if(Dataprotectionprovider! =NULL) {Manager. Usertokenprovider=NewDataprotectortokenprovider<applicationuser,int> (Dataprotectionprovider.create ("ASP . NET Identity")); } returnManager; } }

The main red part of the above has been modified. The main code is here, if rebuild solition will appear a lot of errors, do not panic, slowly find reasons to modify

Parts that may need to be modified

Because the string--->int data conversion, slowly looking for, you will find a lot, ManagerController.cs more and so on.

If you want to see the effect you also need to refer to the data table Userid,roleid has the original nvarchar (128) to int, and set the child growth.

SOURCE Click here to download

How to modify the data type of the Microsoft.AspNet.Identity.EntityFramework.IdentityUser Id field in the ASP. NET Identity Series

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.