Exemplo n.º 1
0
        public HttpResponseMessage Login(string LoginName, string PassWord)
        {
            LoginName = LoginName.ToUpper();
            AccountView account = new AccountView();

            if (string.IsNullOrWhiteSpace(LoginName))
            {
                account.code    = 401;
                account.message = "请输入用户名!";
            }
            if (string.IsNullOrWhiteSpace(PassWord))
            {
                account.code    = 401;
                account.message = "密码错误!";
            }

            UsersView user = this._usersService.FindUsersBy(LoginName);

            if (user != null)
            {
                if (user.UserPwd == PassWord)
                {
                    user.Menu = this._rightsService.GetMenuView(user.Id);
                    string strSource = LoginName + "|" + PassWord + Guid.NewGuid();
                    //获取密文字节数组
                    string token = Encode(strSource);

                    UserDeviceView existsDevice = this._serDeviceService.GetUserDeviceView(LoginName, "win", token);

                    if (existsDevice == null)
                    {
                        this._serDeviceService.CrateUserDevice(LoginName, "win", token);
                    }
                    else
                    {
                        this._serDeviceService.UpdateUserDevice(LoginName, "win", token);
                    }

                    account.code      = 200;
                    account.message   = "Success!";
                    account.token     = token;
                    account.tokenHead = "bearer ";
                    account.User      = user;
                }
                else
                {
                    account.code    = 401;
                    account.message = "密码错误!";
                }
            }
            else
            {
                account.code    = 401;
                account.message = "用户名错误!";
            }
            return(ToJson(account));
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <returns></returns>
        public bool ApiVerification(string userId, string sessionKey)
        {
            UserDeviceView model = this._userDeviceService.GetUserDeviceView(userId, "win", sessionKey);

            if (model == null || model.ExpiredTime <= DateTime.Now)
            {
                return(false);
            }
            else
            {
                this._userDeviceService.UpdateUserDevice(userId, "win", sessionKey);
                return(true);
            }
        }