AngularJS - store basic authentication on login

后端 未结 5 1837
忘掉有多难
忘掉有多难 2021-01-18 07:35

I\'m banging my head on a wall with this - I\'m relatively new to working with APIs and have yet to do anything that requires authentication.

I\'m stuck with sendin

5条回答
  •  萌比男神i
    2021-01-18 08:20

    Use angular-local-storage to store your token in client's local browser when user get successful login.

    angular-local-storage

    $http.post('/token', data).success(function (response) {
        localStorageService.set('authorizationData', { token: response.access_token,});
    });
    

    once you set token in local-storage, you are able to get it for that user.

    like this : localStorageService.get('authorizationData');

    Now use authInterceptorService to set header for each request

    var _request = function (config) {
            config.headers = config.headers || {};
            var authData = localStorageService.get('authorizationData');
            if (authData) {
                config.headers.Authorization = 'Basic' + authData.token;
            }
            return config;
        }
    

    here you get all details : AngularJS Token Authentication using ASP.NET Web API 2, Owin, and Identity

提交回复
热议问题