0

我正在为我的网站使用 Angular。最初,一旦页面启动,就没有任何问题,并且可以完美地进行身份验证。但是,刷新页面时,它不能再拉当前登录用户了。看起来它不调用Windows身份验证。我正在添加我的代码。任何帮助表示赞赏。

intercept(req: HttpRequest<any>, next: HttpHandler):
    Observable<HttpEvent<any>> {

    console.log('AppInterceptorService1111');
    req = req.clone({
        withCredentials: true
      });

    return next.handle(req)
        .pipe(catchError(this.handleError));
}

本地存储已经实现。

this.authenticateCurrentNTLogin().subscribe((data: any) => {
            this.currentUserInfoService.tokenInfo = data.Response;            
            localStorage.setItem('userToken', this.currentUserInfoService.tokenInfo.access_token);
            localStorage.setItem('userRoles', this.currentUserInfoService.tokenInfo.role);        
            this.currentUserInfoService.currentUserTokenExpiration = new Date();
            this.currentUserInfoService.currentUserTokenExpiration.setSeconds(new Date().getSeconds() + parseInt(this.currentUserInfoService.tokenInfo.expires_in));
            localStorage.setItem('myTokenExpiration', this.currentUserInfoService.currentUserTokenExpiration.toLocaleDateString() +
            ' ' + this.currentUserInfoService.currentUserTokenExpiration.toLocaleTimeString());                     
        }, 
4

2 回答 2

1

使用身份验证登录后,将用户 ID 或登录 ID 存储在变量中。在整个应用程序中,您可以检索此局部变量以确认用户 ID 是否在刷新后登录。当他注销时,清除该变量。换句话说,您可以将本地存储用作客户端的会话。当用户登录并验证为 true 时,请使用以下选项。

localStorage.setItem("user", user id)

var user = local storage.getItem("user")

在您正在重新加载的页面中,使用 .get 方法,如果用户不为空,则允许他访问当前页面。如果用户变量为空,则用户未登录并将他重定向到登录页面。当用户注销时:

local storage.removeItem("user")

希望这是您要问的。

于 2020-01-30T04:39:15.333 回答
0

在我的应用程序中,一旦用户进行身份验证,我将用户名和 user_id 存储在 localStorage 中,因此它可以在应用程序的任何位置即时访问。

于 2020-01-30T04:22:08.600 回答