Oauth 2.0 在Postman中复制ADAL.JS-authenticated请求

Oauth 2.0 在Postman中复制ADAL.JS-authenticated请求,oauth-2.0,azure-active-directory,postman,adal,adal.js,Oauth 2.0,Azure Active Directory,Postman,Adal,Adal.js,我有一个.NETWebAPI和一个使用的小型vanilla JS应用程序,我已经设法让它们彼此友好地交谈并正确地进行身份验证 如果我console.log从adalAuthContext.acquireToken()返回的令牌,并在Postman中手动将其作为Authorization:Bearer{{{token}}输入,我还可以从后端获得有效的、经过身份验证的响应 但是,我不知道如何配置Postman内置的OAuth2.0身份验证UI来自动获取令牌。我已经设法以几种方式获得令牌,但后端都不接

我有一个.NETWebAPI和一个使用的小型vanilla JS应用程序,我已经设法让它们彼此友好地交谈并正确地进行身份验证

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

但是,我不知道如何配置Postman内置的OAuth2.0身份验证UI来自动获取令牌。我已经设法以几种方式获得令牌,但后端都不接受

如何配置邮递员以与库相同的方式获取令牌?


为了完整起见,这里有一些代码:

后端配置:

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);
}
const backendUrl = 'http://localhost:55476';
const backendAppId = '<app-id>';

const authContext = new AuthenticationContext({
  clientId: backendAppId,
  tenant: '<tenant>',
  endpoints: [{ [backendAppId]: backendAppId }],
  cacheLocation: 'localStorage'
});

因此,由于Azure AD v1端点不完全符合标准,因此我们不得不以一种稍微奇怪的方式进行操作

邮递员:

  • 在授权下选择OAuth 2.0
  • 单击获取新访问令牌
  • 为授权类型选择隐式
  • 输入应用程序的回复URL作为回调URL
  • 输入类似以下内容的授权URL:
    https://login.microsoftonline.com/yourtenant.onmicrosoft.com/oauth2/authorize?resource=https%3A%2F%2Fgraph.microsoft.com
  • 输入应用程序的应用程序id/客户端id作为客户端id
  • 将作用域和状态保留为空
  • 单击请求令牌
  • 如果配置正确,您将获得一个令牌,邮递员将为您配置授权标头。 现在,关于授权URL。 确保指定AAD租户id或已验证的域名,而不是
    yourtanent.onmicrosoft.com
    。 或者,如果你的应用程序是多租户的,你也可以使用
    common
    资源
    是最重要的参数(不符合标准)。 它告诉AAD您想要访问令牌的API。 在本例中,我为MS Graph API请求了一个令牌,它的资源URI为
    https://graph.microsoft.com
    。 对于您自己的API,您可以使用它们的客户端id或应用程序id URI

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

    authContext.acquireToken(backendAppId, (error, token) => {
       // error handling etc omitted
       fetch(backendUrl, { headers: { Authorization: `Bearer ${token}` } })
           .then(response => response.json())
           .then(console.log)
    })