代码之家  ›  专栏  ›  技术社区  ›  Tomas Aschan

在Postman中复制ADAL.JS认证的请求

  •  0
  • Tomas Aschan  · 技术社区  · 6 年前

    我有一个.NETWebAPI和一个小的vanilla JS应用程序 ADAL.js ,我设法使他们能够友好地交谈并正确地进行身份验证。

    如果我 console.log 从返回的令牌 adalAuthContext.acquireToken() 手动输入为 Authorization: Bearer {{token}} 在Postman中,我还可以从后端获得有效的、经过身份验证的响应。

    如何配置邮递员以相同的方式获取令牌 图书馆有吗?


    为了完整起见,下面是一些代码:

    后端配置:

    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
    
        app.UseWindowsAzureActiveDirectoryBearerAuthentication(
            new WindowsAzureActiveDirectoryBearerAuthenticationOptions
            {
                TokenValidationParameters = new TokenValidationParameters { ValidAudience = "<app-id>" },
                Tenant = "<tenant>",
                AuthenticationType = "WebAPI"
            });
    
        var config = new HttpConfiguration();
        config.MapHttpAttributeRoutes();
        app.UseWebApi(config);
    }
    

    ADAL.js配置:

    const backendUrl = 'http://localhost:55476';
    const backendAppId = '<app-id>';
    
    const authContext = new AuthenticationContext({
      clientId: backendAppId,
      tenant: '<tenant>',
      endpoints: [{ [backendAppId]: backendAppId }],
      cacheLocation: 'localStorage'
    });
    

    authContext.acquireToken(backendAppId, (error, token) => {
       // error handling etc omitted
       fetch(backendUrl, { headers: { Authorization: `Bearer ${token}` } })
           .then(response => response.json())
           .then(console.log)
    })
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   juunas    6 年前

    因此,由于Azure AD v1端点不完全符合标准,我们不得不以一种稍微奇怪的方式来做事情。

    1. 选择 OAuth 2.0版 经授权
    2. 获取新的访问令牌
    3. 选择 隐性的 授予类型
    4. 输入类似于以下内容的授权URL: https://login.microsoftonline.com/yourtenant.onmicrosoft.com/oauth2/authorize?resource=https%3A%2F%2Fgraph.microsoft.com
    5. 输入应用程序的应用程序id/客户端id作为客户端id
    6. 保留作用域和状态为空
    7. 点击请求令牌

    如果配置正确,您将得到一个令牌,邮递员将为您配置授权头。 现在关于授权URL。 yourtenant.onmicrosoft.com . 或者你可以用 common 这个 resource 是最重要的参数(且不符合标准)。 在本例中,我为MS Graph API请求了一个令牌,它的资源URI为 https://graph.microsoft.com 对于您自己的api,您可以使用它们的客户机id或应用程序id URI。

    以下是我设置的屏幕截图:

    Postman OAuth 2 settings

    推荐文章
    Jan69  ·  找不到命名空间
    7 年前