public LoginResultModel Authenticate(JWTInfoModel loginInfo)
        {
            var loginResult = new LoginResultModel();
            var userlogin   = DataTest.UserList.FirstOrDefault(x => x.Username.ToUpper() == loginInfo.Username.ToUpper() && x.Password == loginInfo.Password);

            if (userlogin == null)
            {
                loginResult.Status = LoginStatus.Fail.ToString();
            }
            else
            {
                var role           = DataTest.RoleList.FirstOrDefault(x => x.Id == userlogin.RoleId);
                var listPermission = DataTest.PermissionList.Where(x => x.RoleId == userlogin.RoleId)
                                     .Select(x => x.Function)
                                     .ToList();
                loginInfo.RoleId = userlogin.RoleId;

                var jwtToken = JWTHelper.BuildToken(loginInfo);

                loginResult.Status      = LoginStatus.Success.ToString();
                loginResult.Username    = userlogin.Username;
                loginResult.Role        = role;
                loginResult.Permissions = listPermission;
                loginResult.AccessToken = jwtToken;
            }

            return(loginResult);
        }
Ejemplo n.º 2
0
        public LoginResultModel Authentica(JWTInfoModel input)
        {
            var loginResult = new LoginResultModel();
            var user        = DataTest.Users.Where(u => u.Username == input.Username && u.Password == input.Password).FirstOrDefault();

            if (user == null)
            {
                loginResult.Status = (int)LoginStatus.Fail;
            }
            else
            {
                var userRole        = DataTest.UserRoles.Where(ur => ur.UserId == user.Id).Select(x => x.RoleId).ToList();
                var roles           = DataTest.Roles.Where(r => userRole.Any(ur => ur == r.Id)).ToList();
                var rolePermissions = DataTest.RolePermissions.Where(rp => roles.Any(r => r.Id == rp.RoleId)).Select(rp => rp.PermissionId).ToList();
                var permissions     = DataTest.Permissions.Where(p => rolePermissions.Any(rp => rp == p.Id)).Select(p => p.Function).ToList();
                input.Id = user.Id;
                var jwtToken = JWTHelper.BuildToken(input);

                loginResult.Status      = (int)LoginStatus.Success;
                loginResult.Username    = loginResult.Username;
                loginResult.Roles       = roles;
                loginResult.Permissions = permissions;
                loginResult.AccessToken = jwtToken;
            }
            return(loginResult);
        }