目前我很难弄清楚这件事.
我有一个Azure B2C角SPA应用程序,在前端使用MSAL。在后端,它是带有完整的ASP.NET框架的.NET MVC。
我有一个应用程序在前端(SPA应用程序)和app.UseWindowsAzureActiveDirectoryBearerAuthentication
在后端(ASP.NET MVC)很好地使用ADAL。
现在我正在使用Azure B2C并使用MSAL。
我可以在前端使用MSAL登录,但是后端ASP.NET MVC中配置的中间件不会启动。
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
// Generate the metadata address using the tenant and policy information
MetadataAddress = String.Format(AadInstance, Tenant, DefaultPolicy),
ResponseType = OpenIdConnectResponseType.CodeIdToken,
// These are standard OpenID Connect parameters, with values pulled from web.config
ClientId = ClientId,
RedirectUri = RedirectUri,
PostLogoutRedirectUri = RedirectUri,
// Specify the callbacks for each type of notifications
Notifications = new OpenIdConnectAuthenticationNotifications
{
RedirectToIdentityProvider = OnRedirectToIdentityProvider,
AuthorizationCodeReceived = OnAuthorizationCodeReceived,
AuthenticationFailed = OnAuthenticationFailed,
},
// Specify the claim type that specifies the Name property.
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name"
},
// Specify the scope by appending all of the scopes requested into one string (separated by a blank space)
Scope = $"openid profile offline_access user_impersonation"
}
);
在前端我打电话给:
msalService.loginPopup();
$rootScope.$on("msal:loginSuccess", function () {
console.log("loginSuccess");
var token = msalService.userInfo.idToken;
});
我得到id token
,登录成功.
我在OWIN中间件中的所有通知处理程序中放置了断点:
但是调试器永远不会停止,也就是说,中间件代码不会被调用。
为什么通知处理程序没有被解雇?我是遗漏了什么,还是使用了错误的中间件?
最终,我想要实现的是能够在中间件中提取id_token并执行一些后端附加逻辑\处理。添加自定义声明等。正如我前面提到的那样,我能够使用ADAL
和UseWindowsAzureActiveDirectoryBearerAuthentication
中间件来完成这个任务。在用户登录后,我就有机会连接到中间件的通知。
发布于 2019-07-04 19:27:32
好吧..。事实证明,我正在开发的这个应用程序在前端破坏了cookie,这与MSAL设置的令牌产生了一些冲突。一旦我删除了与cookie相关的代码,那么在发出XHR
请求时就会命中OWIN通知处理程序。
当我检查Chrome工具时,我得出了这个结论,并且看到发送的是一个无效的授权头。它以[Object object]
为价值。
但是,我不得不更改OWIN中间件。现在是app.UseOAuthBearerAuthentication
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
AccessTokenFormat = new JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider(string.Format(AadInstance, Tenant, DefaultPolicy))),
Provider = new OAuthBearerAuthenticationProvider
{
OnValidateIdentity = async context =>
{
try
{
.
.
.
}
}
}
}
现在,每当请求到达后端服务器时,OnValidateIdentity
都会被击中,我有机会检查声明或添加新的声明。
https://stackoverflow.com/questions/56879092
复制相似问题