我已使用adal.js启用通过azure活动目录登录我的vue js应用程序的功能。但是,我想将应用程序限制为该目录内的少数用户。现在,我想知道用户在按下回车键后立即输入的电子邮件地址,这样我就可以检查是否应该允许他访问,即如果允许则将其带到主页,如果拒绝则将其拒绝。
我尝试了以下操作,但是无法获取auth._user。它显示为空。
import * as AuthenticationContext from 'adal-angular/lib/adal';
const config = {
tenant: 'xxxxxxxxxxxx',
clientId: 'xxxxxxxxxxxxxxxxxxxx',
redirectUri: 'http://localhost:1337',
cacheLocation: 'sessionStorage'
};
export default {
authenticationContext: null,
initialize() {
this.authenticationContext = new AuthenticationContext(config);
var auth = this.authenticationContext;
return new Promise((resolve, reject) => {
if (auth.isCallback(window.location.hash) || window.self !== window.top) {
// redirect to the location specified in the url params.
auth.handleWindowCallback();
} else {
// try pull the user out of local storage
let user = auth.getCachedUser();
if (user) {
resolve();
} else {
// no user at all - go sign in.
this.signIn();
}
}
});
},
/**
* @return {Promise.<String>} A promise that resolves to an ADAL token for resource access
*/
acquireToken() {
return new Promise((resolve, reject) => {
this.authenticationContext.acquireToken('https://Atkins.onmicrosoft.com/624c09d9-0646-4699-83b3-d8abdf1f5c47', (error, token) => {
if (error || !token) {
return reject(error);
} else {
return resolve(token);
}
});
});
},
/**
* Issue an interactive authentication request for the current user and the api resource.
*/
acquireTokenRedirect() {
this.authenticationContext.acquireTokenRedirect('https://Atkins.onmicrosoft.com/624c09d9-0646-4699-83b3-d8abdf1f5c47');
},
/**
* @return {Boolean} Indicates if there is a valid, non-expired access token present in localStorage.
*/
isAuthenticated() {
// getCachedToken will only return a valid, non-expired token.
if (this.authenticationContext.getCachedToken(config.clientId)) { return true; }
return false;
},
/**
* @return {Object} An ADAL user profile object.
*/
getUserProfile() {
return this.authenticationContext.getCachedUser().profile;
},
signIn() {
this.authenticationContext.login();
},
signOut() {
this.authenticationContext.logOut();
}
};