我有以下代码:
if (Membership.FindUsersByName(username) == null)
{
Membership.CreateUser(username, password, email);
}
if (!Roles.RoleExists("USR"))
{
Roles.CreateRole("USR");
}
Roles.AddUserToRole(username,"USR");
数据正在aspnet_Users和aspnet_UsersInRoles中插入,但数据没有插入到aspnet_membership中,我需要这些数据,因为我正在使用asp登录控件。
有人可以帮助我吗?
答案 0 :(得分:0)
试一试。我相信你没有传递所有参数。
string userName = txtUserId.Text;
//this value is either encrypted or hashed and is never displayed
string password = txtPassword.Text;
string email = txtEmail.Text;
string passwordQuestion = ddlPasswordQuestion.SelectedValue;
//this value is either encrypted or hashed and is never displayed
string passwordAnswer = txtPasswordAnswer.Text;
MembershipCreateStatus result;
Membership.CreateUser(userName, password, email, passwordQuestion, passwordAnswer, true,out result);
lblResults.Visible = true;
switch (result) {
case MembershipCreateStatus.Success:
txtUserId.Text = null;
txtPassword.Text = null;
txtEmail.Text = null;
ddlPasswordQuestion.SelectedIndex = -1;
txtPasswordAnswer.Text = null;
lblResults.Text = "User successfully created!";
break;
case MembershipCreateStatus.InvalidUserName:
lblResults.Text = "The username format was invalid. Please enter a different username.";
break;
case MembershipCreateStatus.InvalidPassword:
lblResults.Text = "The password was invalid: A password cannot be an empty string and must also meet the pasword strength requirements of the configured provider. Please enter a new password.";
break;
case MembershipCreateStatus.InvalidEmail:
lblResults.Text = "The email format was invalid. Please enter a different username.";
break;
case MembershipCreateStatus.InvalidQuestion:
lblResults.Text = "The password question format was invalid. Please enter a different question.";
break;
case MembershipCreateStatus.InvalidAnswer:
lblResults.Text = "The password answer format was invalid. Please enter a different answer.";
break;
case MembershipCreateStatus.DuplicateUserName:
lblResults.Text = "The username is already in use. Please enter a new username.";
break;
case MembershipCreateStatus.DuplicateEmail:
lblResults.Text = "The email address is already in use. Please enter a different email address.";
break;
default:
lblResults.Text = "An error occurred while creating the user.";
break;
}
}
答案 1 :(得分:0)
问题出在 if 语句中。它不起作用的原因是因为FindUsersByName方法返回MembershipUsers的集合。可能没有任何用户,但仍会返回一个集合。
请将声明更改为以下内容......
if (Membership.FindUsersByName(username).Count == 0)
{
Membership.CreateUser(username, password, email);
}
应该这样做。