[英]How I can create my own password in an empty membership table?
I need to create a user in an empty [aspnet_Membership] table. 我需要在空的[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);
}
I am using ASP.NET authentication to login but it couldn't login to web site because of wrong password. 我正在使用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();
and I have a simple asp.net login control How I can do this ? 而且我有一个简单的asp.net登录控件,该怎么办?
Don't try to change the database directly. 不要尝试直接更改数据库。 If you want to add a new user programmatically, use the Membership.CreateUser method. 如果要以编程方式添加新用户,请使用Membership.CreateUser方法。 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. 除了自己编写登录和注册代码外,您还可以使用内置的登录控件,例如CreateUserWizard控件来添加新向导,使用ChangePassword控件来更改密码等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.