如何使用 AWS Cognito 对 ASP .NET 网站进行身份验证

问题描述 投票:0回答:1

几年前,我使用以下代码通过 AWS Cognito 对 ASP .NET 网站进行身份验证,但这可能不是将密钥放入编译代码中的最佳解决方案。我很好奇推荐的方法是什么?应该使用身份中心吗?或者我应该使用凭证文件?我将如何处理将凭据放在生产服务器上?

string accessKey = "xxxxxxxxxx";
string secretKey = "xxxxxxxxxxx";
string poolId = Environment.GetEnvironmentVariable("cognito_pool_id");
string clientId = Environment.GetEnvironmentVariable("app_client_id");
string clientSecret = Environment.GetEnvironmentVariable("app_client_secret");

var awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
var cognitoClient = new AmazonCognitoIdentityProviderClient(awsCredentials, RegionEndpoint.USEast1);
var userPool = new CognitoUserPool(poolId, clientId, cognitoClient, clientSecret);

builder.Services.AddCognitoIdentity();
builder.Services.AddSingleton<IAmazonCognitoIdentityProvider>(cognitoClient);
builder.Services.AddSingleton<CognitoUserPool>(userPool);

builder.Services
    .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie();

builder.Services
    .ConfigureApplicationCookie(o =>
    {
        o.Cookie.HttpOnly = true;
        o.ExpireTimeSpan = TimeSpan.FromHours(1);
        o.SlidingExpiration = true;
        o.LoginPath = "/Identity/Account/Login";
        o.LogoutPath = "/Identity/Account/Logout";
    });
asp.net amazon-web-services amazon-cognito
1个回答
0
投票

您的网站是否在 AWS EC2 上运行?如果“是”,您根本不应该使用凭证 - 您应该使用“IAM 角色”。在这种情况下,您只需致电 AmazonCognitoIdentityProviderClient cognitoClient = new (); 如果您

在 EC2 上运行 - 您必须将凭证存储在
某处

!是的,代码是最坏的情况 - 每次更改时都需要重新编译代码,并且存在通过版本控制等暴露代码的风险。还有其他方法 - 我们使用 IIS 环境变量;或者也许是 appsettings.json (但同样,存在通过存储库暴露的风险)

© www.soinside.com 2019 - 2024. All rights reserved.