我有一个.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)
})