【问题标题】:How to get AWS Cognito access token with username and password in .NET Core 3.1?如何在 .NET Core 3.1 中使用用户名和密码获取 AWS Cognito 访问令牌?
【发布时间】:2021-05-21 07:35:49
【问题描述】:

我正在使用 .NET Core 3.1 开发 C# 客户端应用程序,该应用程序需要使用 AWS Cognito 用户池进行用户身份验证。

由于这是一个客户端应用程序,我不能使用AdminInitiateAuth 等,只能访问:用户池 ID、客户端 ID 以及用户提供的用户名和密码。

我正在从使用 https://www.npmjs.com/package/amazon-cognito-identity-js 的现有 JavaScript 客户端应用程序移植代码,但我正在努力寻找在 C# 中为 .NET Core 应用程序执行此操作的等效方法。

我确实找到了使用 CognitoAuthentication 扩展库 https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/cognito-authentication-extension.html 看起来很完美的东西,但是在尝试创建提供程序时程序无限期挂起的示例时:

public static async void GetCredsAsync()
{
    AmazonCognitoIdentityProviderClient provider =
        new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials()); // << hangs here
    CognitoUserPool userPool = new CognitoUserPool("poolID", "clientID", provider);
    CognitoUser user = new CognitoUser("username", "clientID", userPool, provider);
    InitiateSrpAuthRequest authRequest = new InitiateSrpAuthRequest()
    {
        Password = "userPassword"
    };

    AuthFlowResponse authResponse = await user.StartWithSrpAuthAsync(authRequest).ConfigureAwait(false);
    accessToken = authResponse.AuthenticationResult.AccessToken;
}

我不确定该示例是否与 .NET Core 相关 - 页面上的第一句话说它是,但顶部有一个横幅说它不是!横幅将我指向https://docs.aws.amazon.com/sdk-for-net/latest/developer-guide/welcome.html,但我似乎无法在那里找到我需要的东西。

如何仅使用用户池 ID、客户端 ID 和用户提供的用户名和密码在 .Net Core 中获取 AWS Cognito 访问令牌?

(或者为什么在示例中创建 AmazonCognitoIdentityProviderClient 挂起?)

感谢您的帮助,谢谢

【问题讨论】:

    标签: c# .net-core amazon-cognito


    【解决方案1】:

    我遇到了 AmazonCognitoIdentityProviderClient 挂起的类似问题,并通过显式设置区域解决了它。

    Amazon.CognitoIdentityProvider.AmazonCognitoIdentityProviderClient provider = new Amazon.CognitoIdentityProvider.AmazonCognitoIdentityProviderClient(
                    new Amazon.Runtime.AnonymousAWSCredentials(), Amazon.RegionEndpoint.USEast1);
    

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      相关资源
      最近更新 更多