在 Javascript 中实现 MSAL

问题描述 投票:0回答:1

我正在开发一个 Cordova 项目,我们正在尝试使用 MSAL JS 库实现 Azure AD 身份验证。我试图遵循其中的工作流程,如果用户尚未登录,我们将调用弹出方法,否则将静默调用获取令牌方法。问题是,每次刷新页面时,即使用户已经登录,它总是会转到弹出身份验证。我们在 Xamarin 中实现了相同的身份验证,但从未遇到同样的问题。

我在 acquireTokenSilent 上收到此错误

The user or administrator has not consented to use the application with ID '' named 'client'. Send an interactive authorization request for this user and resource.

代码:

async function getTokenPopup(request) {
//request.account = account;
return await myMSALObj
.acquireTokenSilent(request)
.then(function(accessTokenResponse){
    console.log("Access Token", accessTokenResponse.accessToken);
})
.catch(async (error) => {
    console.log("silent token acquisition fails.");
    if (error instanceof msal.InteractionRequiredAuthError) {
        console.log("acquiring token using popup");
        return myMSALObj.acquireTokenPopup(request).catch(error => {
            console.error(error);
        });
    } else {
        console.error(error);
    }
 });
}
javascript cordova azure-active-directory msal.js
1个回答
1
投票

为未来遇到同样问题的开发人员分享我如何解决问题。您需要传递包含名称作为提示的silentRequest对象才能使用ssoSilent身份验证。

async function signIn() {
    
    if(isTokenValid()){

        var accounts = myMSALObj.getAllAccounts();

        if(accounts.length === 0 ){
            getTokenPopup(scopeRequest)
                .then(handleSuccessLoginResponse)
                .catch(error => {
                    console.log("Error getting token");
                });
        }
        else{
            silentRequest.loginHint = accounts[0].username;
            handleSilentLogin()
            .then(function(response) {
                handleSuccessLoginResponse(response);
            });
        }
        
    }
   
   
}


async function handleSilentLogin(){
    return myMSALObj.ssoSilent(silentRequest);
}



 async function getTokenPopup(request) {
    return await myMSALObj
    .acquireTokenSilent(request)
    .then(function(accessTokenResponse){
        console.log("Access Token", accessTokenResponse.accessToken);
    })
    .catch(async (error) => {
        console.log("silent token acquisition fails.");
        if (error instanceof msal.InteractionRequiredAuthError) {
            console.log("acquiring token using popup");
            myMSALObj["browserStorage"].clear();
            return myMSALObj.acquireTokenPopup(request).catch(error => {
                console.error(error);
            });
        } else {
            return myMSALObj.acquireTokenPopup(request).catch(error => {
                console.error(error);
            });
        }
    });
}


function isTokenValid(){

    let tokenFromStorage  = localStorage["msalTokenDetails"];
    if(tokenFromStorage){
        let token = JSON.parse(localStorage["msalTokenDetails"]);

        // check if token expired
        if(token.expiresOn > new Date()){
            // returning false since token is already expired
            return false;
        }
        else{
            return true;
        }    
    }
    else{
        return false;
    }
   
}


const silentRequest = {
    loginHint: "[email protected]"
};
© www.soinside.com 2019 - 2024. All rights reserved.