首页 > Web开发 > 详细

asp.net2.0安全性(1)--用户角色篇(代码实现1)--转载来自车老师

时间:2015-09-13 11:38:59      阅读:328      评论:0      收藏:0      [点我收藏+]

 

 

创建用户:

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))//获取用户key主键Membership.DeleteUser(key)

    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;

 }

(车延禄)

收藏于 2007-06-23

asp.net2.0安全性(1)--用户角色篇(代码实现1)--转载来自车老师

原文:http://www.cnblogs.com/ooip/p/4793790.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!