【问题标题】:Get ID Token to forward to another Application获取 ID Token 以转发到另一个应用程序
【发布时间】:2017-05-06 14:59:03
【问题描述】:

我想检索ID Token,我可以在成功验证后通过 GET/POST 请求将其发送到另一个应用程序。

场景如下:

  • 使用不同 url (*.domain.com) 的多个 Web 应用程序
  • 所有应用程序都需要针对 Azure Active Directory 进行身份验证
  • URL 太多,无法全部作为 redirect_url(每个主机名都需要一个)
  • 想法是有一个“登录”应用程序 (login.domain.com) 来处理登录,然后将 ID 令牌转发到 *.domain.com 应用程序(使用状态字段中的 URL)
  • *.domain.com 然后验证 ID Token 并授权用户

使用 Microsoft.AspNetCore.Authentication.OpenIdConnect,我无法弄清楚如何检索 ID 令牌以便正确转发它。

我已经为 ASP.NET Core 1.0 Web 应用程序使用了 VS2015 模板并正确配置了身份验证(这有效)

现在我需要以某种方式获取令牌,但我不知道如何。

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions()
        {
            ClientId = Configuration["Authentication:AzureAd:ClientId"],
            Authority = Configuration["Authentication:AzureAd:AADInstance"] + "Common",
            CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"],

            TokenValidationParameters = new TokenValidationParameters
            {
                // Instead of using the default validation (validating against a single issuer value, as we do in line of business apps),
                // we inject our own multitenant validation logic
                ValidateIssuer = false,

                // If the app is meant to be accessed by entire organizations, add your issuer validation logic here.
                //IssuerValidator = (issuer, securityToken, validationParameters) => {
                //    if (myIssuerValidationLogic(issuer)) return issuer;
                //}
            },
            Events = new OpenIdConnectEvents
            {
                OnTicketReceived = (context) =>
                {
                    // If your authentication logic is based on users then add your logic here
                    return Task.FromResult(0);

                },
                OnAuthenticationFailed = (context) =>
                {
                    context.Response.Redirect("/Home/Error");
                    context.HandleResponse(); // Suppress the exception
                    return Task.FromResult(0);
                },
                // If your application needs to do authenticate single users, add your user validation below.
                //OnTokenValidated = (context) =>
                //{
                //    return myUserValidationLogic(context.Ticket.Principal);
                //}
            }
        });

我想我应该能够使用TicketReceivedContext 在OnTicketReceived 事件中获得它?

【问题讨论】:

    标签: asp.net-core asp.net-core-middleware asp.net-core-identity


    【解决方案1】:

    我使用了错误的事件。 在OnTokenValidated 事件中,您可以访问context.SecurityToken.RawData,这是收到的原始令牌,正是我需要的。

    【讨论】:

      【解决方案2】:

      你也可以试试这个:

      string idToken = string.Empty;
      if (ctx.Properties.Items.ContainsKey(".Token.id_token"))
      {
           idToken = ctx.Properties.Items[".Token.id_token"];
      }
      

      【讨论】:

        猜你喜欢
        • 2013-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-01
        • 2017-06-27
        • 1970-01-01
        • 2019-03-29
        相关资源
        最近更新 更多