Migrating an Existing Website from SQL Membership to ASP.
Public classUser:identityuser { PublicUser () {createdate=DateTime.Now; IsApproved=false; Lastlogindate=DateTime.Now; Lastactivitydate=DateTime.Now; Lastpasswordchangeddate=DateTime.Now; Lastlockoutdate= DateTime.Parse ("1/1/1754"); Failedpasswordanswerattemptwindowstart= DateTime.Parse ("1/1/1754"); Failedpasswordattemptwindowstart= DateTime.Parse ("1/1/1754"); } PublicSystem.Guid ApplicationID {Get;Set; } Public stringMobilealias {Get;Set; } Public BOOLisanonymous {Get;Set; } PublicSystem.DateTime Lastactivitydate {Get;Set; } Public stringMobilepin {Get;Set; } Public stringLoweredemail {Get;Set; } Public stringLoweredusername {Get;Set; } Public stringpasswordquestion {Get;Set; } Public stringpasswordanswer {Get;Set; } Public BOOLisapproved {Get;Set; } Public BOOLIslockedout {Get;Set; } PublicSystem.DateTime CreateDate {Get;Set; } PublicSystem.DateTime Lastlogindate {Get;Set; } PublicSystem.DateTime Lastpasswordchangeddate {Get;Set; } PublicSystem.DateTime Lastlockoutdate {Get;Set; } Public intFailedpasswordattemptcount {Get;Set; } PublicSystem.DateTime Failedpasswordattemptwindowstart {Get;Set; } Public intFailedpasswordanswerattemptcount {Get;Set; } PublicSystem.DateTime Failedpasswordanswerattemptwindowstart {Get;Set; } Public stringComment {Get;Set; } }
Public classSqlpasswordhasher:passwordhasher { Public Override stringHashpassword (stringpassword) { return Base. Hashpassword (password); } Public OverridePasswordverificationresult Verifyhashedpassword (stringHashedpassword,stringProvidedpassword) { string[] passwordproperties = Hashedpassword.split ('|'); if(Passwordproperties.length! =3) { return Base. Verifyhashedpassword (Hashedpassword, Providedpassword); } Else { stringPasswordHash = passwordproperties[0]; intPasswordformat =1; stringSalt = passwordproperties[2]; if(String.Equals (Encryptpassword (Providedpassword, passwordformat, salt), PasswordHash, Stringcomparison.currentcultureignorecase)) {returnpasswordverificationresult.successrehashneeded; } Else { returnpasswordverificationresult.failed; } } }//This was copied from the existing SQL providers and was provided only for Back-compat. Private stringEncryptpassword (stringPassintPasswordformat,stringSalt) { if(Passwordformat = =0)//Membershippasswordformat.clear returnPass; byte[] BIn =Encoding.Unicode.GetBytes (pass); byte[] Bsalt =convert.frombase64string (salt); byte[] BRet =NULL; if(Passwordformat = =1) { //membershippasswordformat.hashedHashAlgorithm HM = Hashalgorithm.create ("SHA1"); if(HM isKeyedHashAlgorithm) {KeyedHashAlgorithm Kha=(KeyedHashAlgorithm) HM; if(Kha. Key.length = =bsalt.length) {kha. Key=Bsalt; } Else if(Kha. Key.length <bsalt.length) {byte[] Bkey =New byte[Kha. Key.length]; Buffer.blockcopy (Bsalt,0, Bkey,0, bkey.length); Kha. Key=Bkey; } Else { byte[] Bkey =New byte[Kha. Key.length]; for(intITER =0; ITER <bkey.length;) { intLen = Math.min (bsalt.length, Bkey.length-ITER); Buffer.blockcopy (Bsalt,0, Bkey, ITER, Len); ITER+=Len; } kha. Key=Bkey; } BRet=Kha.computehash (bIn); } Else { byte[] Ball =New byte[Bsalt.length +Bin.length]; Buffer.blockcopy (Bsalt,0, Ball,0, bsalt.length); Buffer.blockcopy (BIn,0, Ball, Bsalt.length, bin.length); BRet=Hm.computehash (ball); } } returnconvert.tobase64string (BRet); }
Public Usermanager () Base (new userstore<user> (new Applicationdbcontext ())) { thisnew sqlpasswordhasher ();}
PrivateGuid Getapplicationid () {using(SqlConnection connection =NewSqlConnection (configurationmanager.connectionstrings["ApplicationServices"]. ConnectionString)) {stringQueryString ="SELECT ApplicationID from aspnet_applications WHERE applicationname = '/'";//Set application name as in databaseSqlCommand Command=NewSqlCommand (queryString, connection); Command. Connection.Open (); varReader =command. ExecuteReader (); while(reader. Read ()) {returnReader. GetGuid (0); } returnGuid.NewGuid (); } }
var currentapplicationid =new User () {UserName = Username.text,applicationid =currentapplicationid, ...};
Migrating an Existing Website from SQL Membership to ASP.