创建用户:
MembershipCreateStatus mc;
Membership.CreateUser(txtUid.Text, txtPwd.Text, txtEmail.Text, txtQuestion.Text, txtAnswer.Text, true, out mc);
Response.Write(mc.ToString());
删除用户:
if (Membership.DeleteUser(txtUid.Text))
Response.Write("OK");
else
Response.Write("Error");
修改用户信息:
if (User.Identity.IsAuthenticated)
{
MembershipUser user = Membership.GetUser();
user.Email = txtEmail.Text;
Membership.UpdateUser(user);
Response.Write(user.Email.ToString());
}
验证用户登录信息
bool isok = Membership.ValidateUser(txtUid.Text, txtPwd.Text);
if (isok)
{
FormsAuthentication.SetAuthCookie(txtUid.Text, false);
Response.Write("OK");
}
else
{
Response.Write("Error");
}
获取登录用户的信息
if (User.Identity.IsAuthenticated)
{
MembershipUser myuser = Membership.GetUser();
if (myuser != null)
{
Response.Write(myuser.CreationDate);
Response.Write(myuser.Email);
Response.Write(myuser.IsLockedOut);
Response.Write(myuser.IsOnline);
Response.Write(myuser.PasswordQuestion);
Response.Write(myuser.UserName);
}
}
根据密码问题的答案返回用户密码
if (User.Identity.IsAuthenticated)
{
MembershipUser user = Membership.GetUser(txtUid.Text);
txtPwd.Text = user.GetPassword(txtAnswer.Text);
}
修改用户密码
if (User.Identity.IsAuthenticated)
{
MembershipUser user = Membership.GetUser();
user.ChangePassword(txtPwd.Text,txtPWd2.Text);
Response.Write(user.GetPassword(txtAnswer.Text));
}
解锁用户
MembershipUser user = Membership.GetUser(txtUid.Text);
bool b = user.UnlockUser();
Response.Write(b+" "+user.IsLockedOut);
拒绝用户登录
MembershipUser user = Membership.GetUser(txtUid.Text);
user.IsApproved = false;
Membership.UpdateUser(user);
Response.Write(user.IsLockedOut);
允许用户登录
MembershipUser user = Membership.GetUser(txtUid.Text);
user.IsApproved = true;
Membership.UpdateUser(user);
Response.Write(user.IsLockedOut);
根据用户名或邮箱来检索用户
MembershipUserCollection users;
switch(listType.Text)
{
case "姓名":
users = Membership.FindUsersByName(txtFind.Text);
if (users.Count > 0)
{
showUserInfo(users);
}
else
{
Response.Write("未找到用户名");
}
break;
case "邮箱":
users = Membership.FindUsersByEmail(txtFind.Text);
if (users.Count > 0)
{
showUserInfo(users);
}
else
{
Response.Write("未找到邮箱");
}
break;
}
加载所有用户
MembershipUserCollection user = Membership.GetAllUsers();
listUser.DataSource = user;
listUser.DataBind();
加载所有角色
string[] role = Roles.GetAllRoles();
listRole.DataSource = role;
listRole.DataBind();
添加新角色
Roles.CreateRole(txtRole.Text);
删除角色
Roles.DeleteRole(txtRole.Text);
将一个用户添加到一个角色
Roles.AddUserToRole(listUser.Text, listRole.Text);
将一个用户从一个角色中移除
Roles.RemoveUserFromRole(listUser.Text, listRole.Text);
将一个用户加入到多个角色中去
int n = 0;
foreach (ListItem li in listRole.Items)
{
if (li.Selected) n++;
}
string[] roles = new string[n];
int i=0;
foreach(ListItem li in listRole.Items)
{
if(li.Selected)
{
roles[i++] = li.Text;
}
}
Roles.AddUserToRoles(listUser.Text, roles);
将一个用户的所有角色移除
string[] str = Roles.GetRolesForUser(listUser.Text);
Roles.RemoveUserFromRoles(listUser.Text,str);
将多个用户加入到一个角色中去
int n = 0;
foreach (ListItem li in listUser.Items)
{
if (li.Selected) n++;
}
string[] users = new string[n];
int i=0;
foreach (ListItem li in listUser.Items)
{
if (li.Selected)
{
users[i++] = li.Text;
}
}
Roles.AddUsersToRole(users,listRole.Text);
将一个角色中的所有的用户移除
string[] users = Roles.GetUsersInRole(listRole.Text);
Roles.RemoveUsersFromRole(users, listRole.Text);
将多个用户加入到多个角色
int n = 0;
foreach (ListItem li in listUser.Items)
{
if (li.Selected) n++;
}
string[] strUser = new string[n];
n = 0;
foreach (ListItem li in listRole.Items)
{
if (li.Selected) n++;
}
string[] strRoles = new string[n];
for (int i = 0; i < listUser.Items.Count; i++)
{
strUser[i] = listUser.Items[i].Text;
}
for (int i = 0; i < listRole.Items.Count; i++)
{
strRoles[i] = listRole.Items[i].Text;
}
Roles.AddUsersToRoles(strUser, strRoles);