【发布时间】:2025-12-16 12:10:02
【问题描述】:
我有一个带有基于 adal-js 身份验证的 ASP.NET SPA,以及一个带有 Azure Active Directory 身份验证的 ASP.NET Web Api 网站
这两个网站都托管在 Azure 上,例如使用不同的主机名
https://foo.azurewebsites.com/ 和 https://fooapi.azurewebsites.com/
Web Api 网站认证配置为
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
TokenValidationParameters = new TokenValidationParameters() { ValidAudience = ConfigurationManager.AppSettings["ida:Audience"] },
Tenant = ConfigurationManager.AppSettings["ida:Tenant"]
});
}
}
主 SPA adal.js 初始化为:
var config = {
instance: "https://login.microsoftonline.com/",
tenant: "mytenant",
clientId: "client id of foo registration",
postLogoutRedirectUri: "https://foo.azurewebsites.com/",
cacheLocation: "localStorage"
};
authContext = new AuthenticationContext(config);
// Check For & Handle Redirect From AAD After Login
var isCallback = authContext.isCallback(window.location.hash);
authContext.handleWindowCallback();
var errorMessage = authContext.getLoginError();
if (isCallback && !authContext.getLoginError()) {
window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
}
// Check if View Requires Authentication
if (!authContext.getCachedUser()) {
authContext.config.redirectUri = window.location.href;
authContext.login();
return;
}
foo 和 fooapi 的租户相同,客户端 ID 不同(每个应用注册一个)。
foo web 应用程序中的身份验证流程执行成功,但对 fooapi 的每个 http 请求都返回 401 未授权。
如何让 fooapi 分享 foo 的成功认证?
感谢您的任何提示
【问题讨论】:
标签: asp.net azure asp.net-web-api azure-active-directory