【问题标题】:Getting the identity token (id_token) within redirect URI (MVC Controller)在重定向 URI(MVC 控制器)中获取身份令牌 (id_token)
【发布时间】:2019-04-15 14:22:39
【问题描述】:

我希望这主要与 Okta(我们用于社交登录的服务)无关,但我很难找到文档。我正在使用 .NET Core 2.0+,我的 Startup.cs 看起来像这样:

services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    })
    .AddCookie()
    .AddOpenIdConnect(options => 
    {
        //Configuration pulled from appsettings.json by default:
        options.ClientId = Configuration["okta:ClientId"];
        options.ClientSecret = Configuration["okta:ClientSecret"];
        options.Authority = Configuration["okta:Issuer"];
        options.CallbackPath = "/authorization-code/callback";
        options.ResponseType = OpenIdConnectResponseType.IdToken;
        options.SaveTokens = true;
        options.UseTokenLifetime = false;
        options.GetClaimsFromUserInfoEndpoint = true;
        options.Scope.Add("openid");
        options.Scope.Add("profile");
        options.Scope.Add("email");
    });

网站内的登录表单允许您单击“登录 Facebook”,然后通过身份提供商进行身份验证。确认后,它会返回到我定义的重定向“家庭/安全”。当重定向返回时,id_token 在 URL 中作为锚点:

https://localhost:5001/Home/Secure#id_token=XXXXXXX

还有一个授权调用,我可以看到它通过 Chrome 开发人员工具控制台接收到带有 id_token 的响应。我对 .NET Core 不太熟悉,所以我很难理解如何获取这个 id_token。

请求的 Query 或 QueryString 参数中似乎没有 id_token,所以我看不到在哪里可以获取它。

【问题讨论】:

  • # 之后的东西被称为 URI 的“片段”。它纯粹是客户端的,根本不提供给服务器。应该有另一种访问令牌的方法。也许,您对第三方提供商使用了错误的流程。换句话说,这个特定的流程似乎是为 JS 客户端设计的。
  • @ChrisPratt 好的,这样就更有意义了。还有另一个流程,我可以通过 JS 抓取它并将其推送到其他地方。也许这就是我的答案。我会试一试。
  • 您使用的是JS应用中常用的隐式流,如果您的身份服务器允许,您可以使用在Web应用中工作的代码流或混合流。

标签: c# asp.net-core oauth-2.0 claims-based-identity okta


【解决方案1】:

由于您使用的是 OIDC 中间件并将 SaveTokens 设置为 true ,因此您随后可以通过在控制器中为您要访问的 ID 令牌调用 GetTokenAsync 来检索这些令牌:

string idToken = await HttpContext.GetTokenAsync("id_token");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 2019-07-27
    相关资源
    最近更新 更多