首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >针对Azure B2C目录未从MSAL调用的OWIN中间件通知

针对Azure B2C目录未从MSAL调用的OWIN中间件通知
EN

Stack Overflow用户
提问于 2019-07-03 23:39:34
回答 1查看 686关注 0票数 2

目前我很难弄清楚这件事.

我有一个Azure B2C角SPA应用程序,在前端使用MSAL。在后端,它是带有完整的ASP.NET框架的.NET MVC。

我有一个应用程序在前端(SPA应用程序)和app.UseWindowsAzureActiveDirectoryBearerAuthentication在后端(ASP.NET MVC)很好地使用ADAL。

现在我正在使用Azure B2C并使用MSAL

我可以在前端使用MSAL登录,但是后端ASP.NET MVC中配置的中间件不会启动。

代码语言:javascript
运行
复制
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"
}
);

在前端我打电话给:

代码语言:javascript
运行
复制
msalService.loginPopup();

$rootScope.$on("msal:loginSuccess", function () {
    console.log("loginSuccess");

    var token = msalService.userInfo.idToken;
});

我得到id token,登录成功.

我在OWIN中间件中的所有通知处理程序中放置了断点:

  • OnRedirectToIdentityProvider
  • OnAuthorizationCodeReceived
  • OnAuthenticationFailed

但是调试器永远不会停止,也就是说,中间件代码不会被调用。

为什么通知处理程序没有被解雇?我是遗漏了什么,还是使用了错误的中间件?

最终,我想要实现的是能够在中间件中提取id_token并执行一些后端附加逻辑\处理。添加自定义声明等。正如我前面提到的那样,我能够使用ADALUseWindowsAzureActiveDirectoryBearerAuthentication中间件来完成这个任务。在用户登录后,我就有机会连接到中间件的通知。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-04 19:27:32

好吧..。事实证明,我正在开发的这个应用程序在前端破坏了cookie,这与MSAL设置的令牌产生了一些冲突。一旦我删除了与cookie相关的代码,那么在发出XHR请求时就会命中OWIN通知处理程序。

当我检查Chrome工具时,我得出了这个结论,并且看到发送的是一个无效的授权头。它以[Object object]为价值。

但是,我不得不更改OWIN中间件。现在是app.UseOAuthBearerAuthentication

代码语言:javascript
运行
复制
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
    AccessTokenFormat = new JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider(string.Format(AadInstance, Tenant, DefaultPolicy))),

    Provider = new OAuthBearerAuthenticationProvider
    {
        OnValidateIdentity = async context =>
        {
            try
            {
                 .
                 .
                 .
            }
        }
    }
}

现在,每当请求到达后端服务器时,OnValidateIdentity都会被击中,我有机会检查声明或添加新的声明。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56879092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档