Azure active directory 简单ADAL.js示例-需要用户登录

Azure active directory 简单ADAL.js示例-需要用户登录,azure-active-directory,adal,adal.js,Azure Active Directory,Adal,Adal.js,我正在尝试获取一个简单的adal.js示例,以运行一个基本的asp.net核心mvc项目(无授权属性)。当我在下面运行时,我会按预期重定向到AAD,我单击我的用户,然后很快重定向回我的页面,然后返回到AAD登录对话框。我得到一个“需要用户登录”。你知道我做错了什么吗 var variables = { // Domain of Azure AD tenant azureAD: "xx.onmicrosoft.com", // ClientId of Azure AD ap

我正在尝试获取一个简单的adal.js示例,以运行一个基本的asp.net核心mvc项目(无授权属性)。当我在下面运行时,我会按预期重定向到AAD,我单击我的用户,然后很快重定向回我的页面,然后返回到AAD登录对话框。我得到一个“需要用户登录”。你知道我做错了什么吗

var variables = {
    // Domain of Azure AD tenant
    azureAD: "xx.onmicrosoft.com",
    // ClientId of Azure AD application principal
    clientId: "xx-xx-xx-xx"
}

window.config = {
    tenant: variables.azureAD,
    clientId: variables.clientId,
    postLogoutRedirectUri: window.location.origin,
    cacheLocation: "localStorage",
    endpoints: {
        graphApiUri: "https://graph.microsoft.com"
    },
};
var authContext = new AuthenticationContext(config);
var user = authContext.getCachedUser();

if (!user) {
    authContext.login();
}

authContext.acquireToken(config.endpoints.graphApiUri, function (error, token) {
    if (error || !token) {
        console.log("ADAL error occurred: " + error);
        return;
    }
    else {

    }
});

您似乎没有在应用程序中处理来自Azure AD的重定向。您需要调用Adal.js提供的
handleWindowCallback()
方法,以确保解析并缓存重定向URI中Azure AD返回的idtoken,以指示经过身份验证的用户上下文。
对于代码片段,请在带有asp.net后端的普通Javascript应用程序中使用ADAL.js查看此代码。

您使用的ADAL.js版本是什么?是在同一个项目中,还是UI代码与API是一个单独的项目?它在同一个项目中。谢谢,谢谢!