如何解决我正在使用react-adal进行Azure AD单一登录它的令牌在1小时后过期有什么方法可以刷新会话或延长会话过期时间
我已使用 react-adal 库将Azure AD Single Sign In集成到我的企业React应用程序中,用于Azure AD Single Sign In。我已经成功实现了它,但是我正面临一个问题。它的令牌会在1小时后过期,因此它会从React Web应用程序注销。有什么方法可以刷新会话或延长会话过期时间。
import { AuthenticationContext } from 'react-adal';
const config = {
apiUrl: 'someUrl/',graph_access_url: 'https://graph.microsoft.com',graph_access_token_key: 'User_Graph_Token',user_info_key: 'UserInfo'
};
const adalConfig = {
tenant: 'someTenant',clientId: 'someclientId',clientSecret: 'someclientSecret',objectId: 'someObjectId',endpoints: { api: 'someAPI' },cacheLocation: 'localStorage',redirectUri: window.location.origin,azureRootUrl: 'https://login.microsoftonline.com',issuerUrl: 'https://sts.windows.net'
};
const authContext = new AuthenticationContext(adalConfig);
function graphAccessToken() {
return localStorage[config.graph_access_token_key];
}
function azureRequest(url) {
let token = graphAccessToken();
const requestOptions = { method: 'GET',headers: { 'Content-Type': 'application/json',Authorization: 'Bearer ' + token } };
return fetch(url,requestOptions).then(response => response.json());
}
function getMe() {
return azureRequest('https://graph.microsoft.com/v1.0/me');
}
function login() {
authContext.login();
}
export function logout() {
localStorage.setItem(config.user_info_key,'');
localStorage.setItem(config.graph_access_token_key,'');
localStorage.clear();
authContext.logOut();
}
authContext.handleWindowCallback();
if (window === window.parent) {
if (!authContext.isCallback(window.location.hash)) {
if (authContext.getCachedToken(authContext.config.clientId) || authContext.getCachedUser()) {
authContext.acquireToken('https://graph.microsoft.com',(error,id_token) => {
if (id_token) {
localStorage.setItem(config.graph_access_token_key,id_token);
if (localStorage.getItem('adal.idtoken')) {
// Some Logic Implemented here.
}
}
});
}
}
}
解决方法
react-adal
使用iframe进行令牌静默刷新,您需要按以下方式使用index.js
。
您生成的第一个令牌的寿命为1小时,当该令牌即将到期时,库将检索刷新令牌。默认情况下,该库将尝试在当前令牌到期日期之前至少5分钟刷新令牌。
index.js :
import { runWithAdal } from 'react-adal';
import { authContext } from './adalConfig';
const DO_NOT_LOGIN = false;
runWithAdal(authContext,() => {
require('./indexApp.js');
},DO_NOT_LOGIN);
有关更多详细信息,请参见blog的前端部分。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。