如何在空的成员资格表中创建自己的密码?

时间:2011-04-14 10:56:20

标签: c# asp.net asp.net-membership membership-provider

我需要在空的[aspnet_Membership]表中创建一个用户。

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);
}

我使用ASP.NET身份验证登录但由于密码错误而无法登录网站。

<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();        

我有一个简单的asp.net登录控件如何才能做到这一点?

1 个答案:

答案 0 :(得分:0)

不要尝试直接更改数据库。如果要以编程方式添加新用户,请使用Membership.CreateUser方法。该方法的文档包含一个示例,其中包含您需要编写的代码。

除了自己编写登录和注册代码外,您还可以使用内置login controls(如CreateUserWizard控件添加新向导,ChangePassword控件更改密码等