繁体   English   中英

如何使用 .NET 生成带有 OIDC 集成的 AWS Cognito 登录 URL?

[英]How can I generate an AWS Cognito login URL w/ my OIDC integration using .NET?

我的 Blazor 应用程序在自动将用户重定向到 AWS Cognito 登录方面做得很好,但它如何生成登录 URL? 我想创建一个登录按钮,用户可以单击该按钮将他们带到 AWS Cognito,但事实证明这很困难,因为我不知道如何生成其他一些 URL 查询参数,例如code_challenge 是否有某种 OIDC/Identity 服务我可以通过依赖注入实例化以生成登录 URL?

TLDR:.NET OIDC 如何生成登录 URL 有很多神奇之处,我想弄清楚如何重用该魔法而无需编写代码来手动生成 Cognito 登录 URL。 .NET OIDC 中是否内置了某种 function 或将用户重定向到配置的登录门户的身份验证服务?

这是我的Program.cs中的 OIDC 配置


builder.Services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    })
    .AddCookie()
    .AddOpenIdConnect(options =>
    {
        options.ResponseType = builder.Configuration["Authentication:Cognito:ResponseType"];
        options.MetadataAddress = builder.Configuration["Authentication:Cognito:MetadataAddress"];
        options.ClientId = builder.Configuration["Authentication:Cognito:ClientId"];
    })
    .AddJwtBearer(options =>
    {
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidIssuer = builder.Configuration["Authentication:Cognito:CognitoDomain"],
            ValidateIssuerSigningKey = true,
            ValidateIssuer = true,
            ValidateLifetime = true,
            ValidAudience = builder.Configuration["Authentication:Cognito:ClientId"],
            ValidateAudience = false
        };
        
        options.MetadataAddress = builder.Configuration["Authentication:Cognito:MetadataAddress"];
    });

如果要控制托管 web UI 的登录 URL 中包含哪些参数,请手动构建 URL。

  1. 在 Amazon Cognito 控制台中,选择管理用户池,然后选择您的用户池。
  2. 在左侧导航窗格中,选择应用集成。
  3. 将域 URL 复制到剪贴板,然后将其粘贴到文本编辑器中以供参考。
  4. 在左侧导航窗格中的应用集成下,选择应用客户端设置。
  5. 执行以下操作:在 App client [name] 下,将 ID 复制到剪贴板,然后将其粘贴到文本编辑器中以供参考。 在登录和注销 URL 下,复制您为回调 URL 输入的 URL。
  6. Construct the URL for the hosted web UI by pasting together the information that you just copied into this format: domainUrl/login?response_type=code&client_id=appClientId&redirect_uri=callbackUrl For example: https://my-user-pool.auth.us-east -1.amazoncognito.com/login?response_type=code&client_id=a1b2c3d4e5f6g7h8i9j0k1l2m3&redirect_uri=https://my-website.com

If you enabled Authorization code grant earlier for Allowed OAuth Flows, then using this URL prompts Amazon Cognito to return an authorization code when your users sign in. If you enabled Implicit grant for Allowed OAuth Flows earlier and you want Amazon Cognito to return an access token相反,当您的用户登录时,然后在 URL 中将 response_type=code 替换为 response_type=token。

OIDC 重定向的代码可以很容易地用任何语言编写。 例如,请参阅此 Javascript 代码以获取客户端流程,您可以轻松地将其转换为 C#。 对于执行此类操作的 C# 库,请参阅Identity Model oidc 客户端,这对于借用代码片段(例如 PKCE 值)很有用。

最后,Blazor 应用程序仍然是基于浏览器的应用程序,因此应遵循当前最佳实践,将令牌排除在浏览器之外。 前端的后端(例如实用程序 API)通常会提供重定向 URL 到浏览器前端, 如此代码示例中所示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM