Asp.net web api SPA应用程序中的AAD令牌可以调用Nodejs API,但不能调用.Net WebAPI
我有一个SPA应用程序,它使用MSAL从AAD获取令牌。因为MSAL与v2端点一起工作,而且v2端点当前不支持为自定义API发出令牌,所以我将ID令牌传递给我的API,并将我的API本质上视为同一个应用程序。(虽然这有一种味道,但它确实可以工作——至少在NodeJSAPI中是这样) 水疗应用程序Asp.net web api SPA应用程序中的AAD令牌可以调用Nodejs API,但不能调用.Net WebAPI,asp.net-web-api,azure-active-directory,msal,Asp.net Web Api,Azure Active Directory,Msal,我有一个SPA应用程序,它使用MSAL从AAD获取令牌。因为MSAL与v2端点一起工作,而且v2端点当前不支持为自定义API发出令牌,所以我将ID令牌传递给我的API,并将我的API本质上视为同一个应用程序。(虽然这有一种味道,但它确实可以工作——至少在NodeJSAPI中是这样) 水疗应用程序 let idToken = Msal.Storage('localStorage').getItem(Msal.Constants.idTokenKey); this.http.configure(c
let idToken = Msal.Storage('localStorage').getItem(Msal.Constants.idTokenKey);
this.http.configure(config => {
config.withBaseUrl("http://localhost:3001/")
config.withDefaults({headers: {'Authorization': 'Bearer ' + idToken}})
});
//Call API
this.http.fetch("account")
...
Node.js API
//Using express/passport
var BearerStrategy = require("passport-azure-ad").BearerStrategy;
var options = {
identityMetadata: "https://login.microsoftonline.com/tenantid/.well-known/openid-configuration/",
clientID: "xxxxxxx-xxxx-xxxxxxx-xxxxx",
passReqtoCallback: false,
validateIssuer: true,
issuer: "http://login.microsoftonline.com/{tenantid}/v2.0"
};
app.get("/account",passport.authenticate('oauth-bearer',{session: false}),...
以上所有工作。一旦用户使用SPA进行了身份验证,就会传递令牌,对节点API的调用也会起作用
我现在正试图用.Net WebAPI替换Nodejs API。我有以下资料:
Startup.cs
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
TokenValidationParameters = new TokenValidationParameters
{
//Same ID as used for ClientID in Nodejs
ValidAudience = "xxxxxx-xxxxx-xxxxx-xxxxx",
ValidIssuer = "https://login.microsoftonline.com/{tenantid}/v2.0",
ValidateIssuer = true,
AuthenticationType = "WebApi" //Tried both with and without this
},
Tenant = "{tenantid}" //have tried both id and name
}
)
[Authorize]
[Route("account")]
public IHttpActionResult AccountProfile(){
//Get Account information
....
return Ok(profile);
}
AccountController.cs
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
TokenValidationParameters = new TokenValidationParameters
{
//Same ID as used for ClientID in Nodejs
ValidAudience = "xxxxxx-xxxxx-xxxxx-xxxxx",
ValidIssuer = "https://login.microsoftonline.com/{tenantid}/v2.0",
ValidateIssuer = true,
AuthenticationType = "WebApi" //Tried both with and without this
},
Tenant = "{tenantid}" //have tried both id and name
}
)
[Authorize]
[Route("account")]
public IHttpActionResult AccountProfile(){
//Get Account information
....
return Ok(profile);
}
但是,当我指向SPA应用程序调用.Net api时,我总是会得到对此请求的授权被拒绝
有什么我遗漏的吗
编辑
顺便说一下,我已经检查了正在使用的令牌
我用于clientID
(Nodejs)和validudience
(.Net)的值与令牌中的aud
声明完全匹配。颁发者
(Nodejs)和ValidIssuer
(.Net)与令牌中的iss
声明完全匹配。最后,在我的代码中插入{tenantid}的任何地方,实际值都与令牌中的tid
声明完全匹配。我们在从ADAL切换到MSAL时遇到了类似的问题,并使用类似的方法使其工作,如。具体看一下这些文件:
更新:我们的Startup.cs:
var provider = new OpenIdConnectCachingSecurityTokenProvider(
string.Format(bc2Instace, tenant, policyId));
var jwt = new JwtFormat(clientId, provider);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
AccessTokenFormat = jwt,
});
谢谢,我试试看。令人沮丧的是,使用Nodejs/Passport与AAD集成比在.Net中与MS自己的中间件集成更直观。为实现这一点,微软正在努力。我的目标是.NET4.6.1。CustomValidatingJwtFormat
类派生自JwtFormat类,该类从System.IdentityModel.Tokens
获取其TokenValidationParameters
。编译器说,System.IdentityModel.Tokens
不包含TokenValidationParameters
的定义Microsoft.IdentityModel.Tokens
确实包含一个定义,但随后我会出错,因为编译器无法将Microsoft.IdentityModel.Tokens
转换为System.IdentityModel.Tokens
有什么方法可以解决这个问题吗?请参阅我的更新答案。不幸的是,我没有深入了解它是如何工作的。谢谢你的帮助。不幸的是,我在兜圈子。正如我所说,System.IdentityModel.Tokens
和Microsoft.IdentityModel.Tokens
库之间存在冲突。Microsoft库是我正在使用的Microsoft.Owin
库的依赖项,系统库是JwtFormat
类所必需的。但两者的加入都会导致冲突。您提到的类出现在nuget包“System.IdentityModel.Tokens.Jwt”中。这也是我们得到它的地方。你需要使用MSAL吗?阿达尔似乎更容易在这一点上的时间整合。