简体   繁体   中英

How I can create my own password in an empty membership table?

I need to create a user in an empty [aspnet_Membership] table.

public string EncodePassword(string pass, string salt)
{       
    byte[] bIn = Encoding.Unicode.GetBytes(pass);
    byte[] bSalt = Convert.FromBase64String(salt);
    byte[] bAll = new byte[bSalt.Length + bIn.Length];
    byte[] bRet = null;
    System.Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);
    System.Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);
    System.Security.Cryptography.HashAlgorithm s = System.Security.Cryptography.HashAlgorithm.Create(Membership.HashAlgorithmType);
    bRet = s.ComputeHash(bAll);          
    return Convert.ToBase64String(bRet);
}

I am using ASP.NET authentication to login but it couldn't login to web site because of wrong password.

<membership>
  <providers>
    <clear />
    <add name="AspNetSqlMembershipProvider" 
         type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
         connectionStringName="SQLConnectionString"
         enablePasswordRetrieval="false" 
         enablePasswordReset="true" 
         requiresUniqueEmail="false"
         requiresQuestionAndAnswer="false" 
         minRequiredPasswordLength="3" 
         minRequiredNonalphanumericCharacters="0"
         applicationName="eShop"           
         passwordFormat="Hashed" />
  </providers>
</membership>

    password = EncodePassword(password, "asas" + password);    
    string userId = "0";
    SqlCommand c = new SqlCommand();
    c.Connection = con;
    cmd.CommandText = "Select UserId From aspnet_Users Where UserName ='" + username + "'";
    userId = ((int)cmd.ExecuteScalar()).ToString();
    cmd.CommandText = string.Empty;
    c.CommandText = "insert InTo aspnet_Membership ([ApplicationId], [UserId], [Password], [PasswordSalt], [PasswordFormat]) ";
    c.CommandText += "Values (2 , " + userId + ",'" + password + "','" + password + "', 1)";
    c.ExecuteNonQuery();        

and I have a simple asp.net login control How I can do this ?

Don't try to change the database directly. If you want to add a new user programmatically, use the Membership.CreateUser method. The method's documentation contains an example with the code you need to write.

Instead of writing the login and registration code yourself you can also use the built-in login controls like the CreateUserWizard control to add a new wizard, the ChangePassword control to change a password etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM