【问题标题】:Token based authentication for both Web App and Web API using Azure AD B2C使用 Azure AD B2C 对 Web 应用程序和 Web API 进行基于令牌的身份验证
【发布时间】:2017-08-07 10:38:34
【问题描述】:

场景: Web 应用程序和 Web API 都需要从服务器端进行身份验证和保护。

要求: Web 应用程序为浏览器提供内容,浏览器应该直接调用 Web API(即浏览器到 API)。

问题: 是否可以同时使用令牌对 Web APP 和 API 进行身份验证?

任何示例代码或明确的方向都将受到高度赞赏。


通常 Web 应用程序使用 cookie 进行身份验证,API 使用令牌进行身份验证。有一些可用的示例项目 here 但它们要么是浏览器到 API(基于 SPA 令牌),要么是服务器端 Web 应用程序从服务器到服务器调用 API。

更新 1

应用程序正在保存TokenValidationParameters 并在应用程序控制器中使用bootstrapContext.Token 来获取服务器到服务器的通信。

根据@dsrockis,我正试图在验证结束后不久从 Web 应用程序中获取id_token(不在应用程序控制器内)。

我在Startup 类中的OpenIdConnectAuthenticationOptions.Notifications 中使用SecurityTokenValidated 调用程序。 SecurityTokenValidated 接收到 SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> 类型的参数,但我不确定在哪里可以找到 id_token。方法如下。

private OpenIdConnectAuthenticationOptions CreateOptionsFromPolicy(string policy)
{
    return new OpenIdConnectAuthenticationOptions
    {
        // For each policy, give OWIN the policy-specific metadata address, and
        // set the authentication type to the id of the policy
        MetadataAddress = String.Format(aadInstance, tenant, policy),
        AuthenticationType = policy,

        // These are standard OpenID Connect parameters, with values pulled from web.config
        ClientId = clientId,
        RedirectUri = redirectUri,
        PostLogoutRedirectUri = redirectUri,
        Notifications = new OpenIdConnectAuthenticationNotifications
        {
            AuthenticationFailed = OnAuthenticationFailed,

            //NEW METHOD INVOKE ************************************
            //******************************************************
            SecurityTokenValidated = OnSecurityTokenValidated

            //******************************************************
        },
        Scope = "openid",
        ResponseType = "id_token",

        TokenValidationParameters = new TokenValidationParameters
        {
            NameClaimType = "name",
            SaveSigninToken = true
        },
    };
}



//NEW METHOD ************************************
private Task OnSecurityTokenValidated(
       SecurityTokenValidatedNotification<OpenIdConnectMessage,
                       OpenIdConnectAuthenticationOptions> arg)
{
    //QUESTION ********************************************************
    //How to find the just saved id_token using incoming parameter, arg
    //*****************************************************************

    return Task.FromResult(0);
}

更新 2

我尝试了AuthorizationCodeReceived 而不是SecurityTokenValidated,但它根本没有被调用。正如这里所讨论的,我的redirect url does have an ending slash 也是如此。

有什么想法吗?

【问题讨论】:

    标签: authentication asp.net-mvc-5 asp.net-web-api2 azure-ad-b2c http-token-authentication


    【解决方案1】:

    我们支持 AAD B2C 的 ASP.NET OpenID Connect 中间件的构建依赖于来自浏览器的 cookie 身份验证。它不接受标头中的令牌或任何类似的用于保护网页的内容。所以我想说,如果你想以经典方式从你的网络应用程序中提供 HTML,你需要使用 cookie 来验证对网络应用程序的请求。

    您绝对可以在浏览器中获取和存储令牌并使用这些令牌访问您的 Web API,即使您使用 cookie 对 Web 应用程序进行身份验证也是如此。我推荐两种模式:

    • 使用 OpenID Connect 中间件执行初始登录,从服务器端启动流程,如示例中所述。流程完成后,中间件将验证生成的 id_token 并将 cookie 放入浏览器中以供将来请求。您可以使用编写here 的代码行指示中间件保存id_token 以供以后使用。然后,您可以以某种方式将该 id_token 传递到您的浏览器,缓存它,并使用它向 API 发出请求。
    • 另一种模式是相反的。首先使用 B2C 文档中的单页应用程序模式从 javascript 启动登录。在浏览器中缓存生成的 id_tokens,并使用它们进行 API 调用。但是当登录完成时,您可以向您的 Web 应用程序发送一个带有 id_token 的请求,触发 OpenID Connect 中间件来处理请求并发出会话 cookie。如果您想知道该请求的格式,我建议您检查常规的服务器端 OpenID Connect 流程。

    【讨论】:

      【解决方案2】:

      找到了我自己的问题的答案,并在此处添加以供将来参考。

      验证成功后,可以通过调用SecurityTokenValidated 通知来访问id_token。代码示例如下。

      private Task OnSecurityTokenValidated(
             SecurityTokenValidatedNotification<OpenIdConnectMessage,
                             OpenIdConnectAuthenticationOptions> arg)
      {
          //Id Token can be retrieved as below.
          //**************************************
          var token = arg.ProtocolMessage.IdToken;
      
          return Task.FromResult(0);
      }
      

      但是,将其直接保存到浏览器 cookie 中可能并不安全。

      【讨论】:

      • 你能解释一下吗?这个方法什么时候执行?您如何实施解决方案?
      • 所有这些都在 Web 应用程序(而不是 API)的 App_Start 目录中的 Startup.Auth.cs 中的 Startup 类中完成。您需要先对 Web App 和 API 的身份验证进行梳理。在原始帖子中有一个示例项目的链接。
      猜你喜欢
      • 2021-03-14
      • 2020-03-19
      • 1970-01-01
      • 2017-04-08
      • 2016-11-15
      • 1970-01-01
      • 2022-01-06
      • 2018-07-19
      • 1970-01-01
      相关资源
      最近更新 更多