【问题标题】:OperationCanceledException when trying to authenticate user in AWS Cognito尝试在 AWS Cognito 中对用户进行身份验证时出现 OperationCanceledException
【发布时间】:2019-09-28 23:48:08
【问题描述】:

我正在使用 AWS Cognito,我正在尝试从我的 API 后端对用户进行身份验证。

但我收到“操作已取消”。异常,我不知道为什么!

这是我的代码:

 var authReq = new AdminInitiateAuthRequest()
 {
     UserPoolId = this.UserpoolId,
     ClientId = this.ClientId,
     AuthFlow = AuthFlowType.ADMIN_NO_SRP_AUTH
 };
 authReq.AuthParameters.Add("USERNAME", username);
 authReq.AuthParameters.Add("PASSWORD", password);

 try
 {
     AdminInitiateAuthResponse authResp = await 
     this.SecClient.AdminInitiateAuthAsync(authReq);
 }
 catch(Exception e)
 {
     //The operation was canceled exception
 }

我的堆栈跟踪

在 System.Net.Http.HttpClient.HandleFinishSendAsyncError(异常 e, CancellationTokenSource cts)\r\n 在 System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)\r\n at System.Net.Http.HttpClient.GetStringAsyncCore(Task1 getTask)\r\n at Amazon.Runtime.Internal.Util.AsyncHelpers.c__DisplayClass1_11.<<RunSync>b__0>d.MoveNext()\r\n --- End of stack trace from previous location where exception was thrown ---\r\n at Amazon.Runtime.Internal.Util.AsyncHelpers.ExclusiveSynchronizationContext.BeginMessageLoop() in D:\\JenkinsWorkspaces\\trebuchet-stage-release\\AWSDotNetPublic\\sdk\\src\\Core\\Amazon.Runtime\\Internal\\Util\\_mobile\\AsyncHelpers.cs:line 142\r\n at Amazon.Runtime.Internal.Util.AsyncHelpers.RunSync[T](Func1 任务)在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\_mobile\AsyncHelpers.cs:line 87\r\n 在 Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri uri, TimeSpan 超时,IWebProxy 代理)在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\AWSSDKUtils.cs:line 1008\r\n 在 Amazon.Util.EC2InstanceMetadata.GetItems(字符串 relativeOrAbsolutePath,Int32 尝试,布尔 slurp)在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\_bcl+netstandard\EC2InstanceMetadata.cs:line 513\r\n 在 Amazon.Util.EC2InstanceMetadata.get_IAMSecurityCredentials() 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\_bcl+netstandard\EC2InstanceMetadata.cs:line 311\r\n 在 Amazon.Runtime.DefaultInstanceProfileAWSCredentials.FetchCredentials() 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\_bcl+netstandard\DefaultInstanceProfileAWSCredentials.cs:line 142\r\n 在 Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentials() 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\_bcl+netstandard\DefaultInstanceProfileAWSCredentials.cs:line 88\r\n 在 Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentialsAsync() 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\_bcl+netstandard\DefaultInstanceProfileAWSCredentials.cs:line 106\r\n 在 Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext 执行上下文)在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\CredentialsRetriever.cs:line 90\r\n 在 Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)\r\n 在 Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext 执行上下文)在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\RetryHandler\RetryHandler.cs:line 137\r\n 在 Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)\r\n 在 Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)\r\n 在 Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)\r\n 在 Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)\r\n 在 KaiserSmith.MS.Security.AWSCognitoAdapter.UserLogin(字符串用户名, 字符串密码)在 C:\Users\Jason\Dropbox\Development\KaiserSmith.MS\BundleShared\Security\AWSCognitoAdapter.cs:line 83" 字符串

【问题讨论】:

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


    【解决方案1】:

    我自己设法解决了这个问题。正如弗拉基米尔在他的回答中指出的那样,我首先开始研究网络错误。我已经排除了本地机器上的防火墙问题,所以几个小时后我在不同的机器上尝试了同样的问题。

    我尝试用谷歌搜索类似的问题,但都没有运气。 但经过大量调查,我最终找到了原因,尽管该异常似乎具有误导性。 在我的谷歌搜索过程中,我遇到了其他以不同方式启动客户端的示例,所以我尝试了这个和宾果游戏!

    所以我改变了这个:

    this.SecClient = new AmazonCognitoIdentityProviderClient(Amazon.RegionEndpoint.EUCentral1);
    

    变成这样

    this.SecClient = new AmazonCognitoIdentityProviderClient(myAwsAccesskey, myAwsSecret,  Amazon.RegionEndpoint.EUCentral1);
    

    这似乎已经解决了问题!

    【讨论】:

      【解决方案2】:

      让我们遍历堆栈跟踪和相关的源代码:

      System.Net.Http.HttpClient.HandleFinishSendAsyncError
      System.Net.Http.HttpClient.FinishSendAsyncUnbuffered
      ..
      System.Net.Http.HttpClient.GetStringAsyncCore(Task)
      ..
      SendAsync
      ..
      AWSSDKUtils.cs:line 1008
      Amazon.Util.EC2InstanceMetadata.GetItems
      ..

      cts (CancellationTokenSource) 被取消时抛出的OperationCanceledExceptionctsHttpClient.Timeout expired or cancelled pending request 时被取消。

      HttpClient.Timeout发起请求assigns the custom value的HttpClient。 Amazon.Util.EC2InstanceMetadata.GetItems 中的超时设置为 5 秒

      结论:这个请求没有时间执行 5 秒。可能是 AWS Cognito 暂时出现问题或某些网络延迟问题。

      建议:

      • 如果此故障是暂时性的,并且可能会在短暂延迟后自行纠正,那么应用 Polly.RetryPolicy

      • 是有意义的
      • 尝试研究网络延迟。

      【讨论】:

        【解决方案3】:

        在我的情况下,.NET Cognito 客户端正在抛出 OperationCancelledException 或 SocketException(主机已关闭)。

        因为您没有向 AmazonCognitoIdentityProviderClient 的构造函数提供任何 AWS 凭证,它尝试向 http://169.254.169.254 发送 HTTP 请求以获取 EC2 实例元数据(最终尝试获取 EC2 实例配置文件或类似的 IAM 角色,它应该假设)。

        解决方案是向构造函数提供无效的 AWS 凭证。 new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.BasicAWSCredentials(@"Invalid", @"Credentials"))

        套接字异常:当 169.254.169.254 被路由出我的以太网接口并且无法获得 ARP 响应时,观察到主机已关闭。

        【讨论】:

          猜你喜欢
          • 2017-04-26
          • 1970-01-01
          • 2018-12-09
          • 2021-06-25
          • 2016-09-27
          • 2018-07-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多