[英]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。
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.