代码之家  ›  专栏  ›  技术社区  ›  Thomas Segato

简单ADAL。js示例-需要用户登录

  •  2
  • Thomas Segato  · 技术社区  · 8 年前

    我想找一个简单的adal。使用基本asp运行的js示例。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 {
    
        }
    });
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Navya Canumalla    8 年前

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