【发布时间】:2018-12-28 08:15:07
【问题描述】:
我正在关注this 的文章。作者正在使用fake-backend.ts拦截一个以/api/authenticate结尾的api调用,并根据用户名和密码从静态数组中检查用户。
当用户使用用户名和密码提交表单时,它会调用authentication.service.ts 登录函数,该函数在路径/api/authenticate 上发布调用,并且由于调用的url 以/api/authenticate 结尾,所以现在fake-backend.ts 发挥作用正在从静态数组验证用户。身份验证成功后,它会生成一个令牌并控制返回到authentication.service.ts 以映射响应对象
我通过调用 api 来验证来自 db 的用户来更改此 fake-backend.ts。但我遇到了错误。
https://localhost:44348/api/authenticate 的 Http 失败响应: 404 确定
也调用不返回authentication.service.ts 来映射响应对象。
if (request.url.endsWith('/api/authenticate') && request.method === 'POST') {
const UserName = request.body.username;
const Password = request.body.password;
var credentials = { UserName, Password };
//return this.employeeService.autheticateEmployeeByCredentials(credentials);
this.employeeService.autheticateEmployeeByCredentials(credentials).subscribe((res) => {
if (res == 1)
return of(new HttpResponse({ status: 200, body: { token: 'fake-jwt-token' } }));
else
return throwError({ error: { message: 'Username or password is incorrect' } });
});
}
编辑:
我正在使用 api 控制器并从返回 1 进行验证,如果它存在于 db else 0 中。也从 api 获得响应,但控制不会返回到 authentication.service.ts,而是转到 login.component.ts 订阅并抛出此错误。
【问题讨论】:
-
你看过这些博客吗? jasonwatmore.com/post/2016/08/16/…blog.angular-university.io/angular-jwt-authentication 另一个了解 JSON Web 令牌的好资源:blog.angular-university.io/angular-jwt
-
你的后端是什么,在哪里?
-
我已经更新了问题请检查编辑部分..
标签: angular jwt asp.net-core-2.0 angular6 jwt-auth