布衣天使

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、开发背景:

  在用户登录的时候,验证用户和密码是否正确。验证通过后将用户名和用户ID保存下来以便后续数据更新时使用。

二、用户验证方法:

  1、创建DBContext 对象。

   ApplicationDbContext _context;

   //Controller构造函数
        public FramController(ApplicationDbContext context)
        {
            _context = context;
        }

  2、查询数据库。

  var users = from u in _context.UserInfo
                                where u.User_Account == "system"
                                select u;

  3、验证用户密码是否正确,并获取用户ID。

  if(users.Count()>0)
                    {
                        String pwd = users.First().User_Pwd;  //获取用户密码
                        if (pwd == Command.Md5Helper(ul.Password))  //验证密码是否正确
                            Command.UserId = users.First().User_ID.ToString();  //获取用户ID
                        else  //验证失败返回用户登录页面并显示“用户名或密码错误!”
                            return Redirect("/Home/Index?UserName=" + ul.UserAccount + "&log= username or password erro!"+Command.Md5Helper(ul.Password));
                    }

posted on 2016-05-21 14:34  布衣天使  阅读(737)  评论(0编辑  收藏  举报