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


     

    创建用户:

    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

  • 相关阅读:
    sqlserver2012附加数据库2005版本时出现的问题
    jQuery实现评论还剩多少个字
    在mvc3中经常使用身份验证实现
    Windows Phone学习(1):棋子未动,先观全局
    使用jquery.pagination.js实现无刷新分页
    Javascript和JQuery中常用的随机数产生函数(很好用)
    网页打印样式设置(页眉,页脚,页边距)
    rdlc到设置宽度后自动换行(转)
    VSS忘记Admin密码和修改默认登陆用户
    解决jquery 修改onclick事件后IE兼容模式下立刻执行的问题
  • 原文地址:https://www.cnblogs.com/ooip/p/4793790.html
Copyright © 2020-2023  润新知