C# 尝试在AWS Cognito中对用户进行身份验证时,OperationCanceledException

C# 尝试在AWS Cognito中对用户进行身份验证时,OperationCanceledException,c#,.net-core,amazon-cognito,C#,.net Core,Amazon Cognito,我正在玩AWS Cognito,我正在尝试从我的API后端验证一个用户 但是我得到了一个“手术被取消”的例外,我不知道为什么 这是我的密码: var authReq = new AdminInitiateAuthRequest() { UserPoolId = this.UserpoolId, ClientId = this.ClientId, AuthFlow = AuthFlowType.ADMIN_NO_SRP_AUTH }; authReq.Auth

我正在玩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(任务
1 sendTask,
HttpRequestMessage请求,CancellationTokenSource cts,布尔值
处置)\r\n位于
System.Net.Http.HttpClient.GetStringAsyncCore(任务
1 getTask)\r\n位于 Amazon.Runtime.Internal.Util.AsyncHelpers.c\u\u DisplayClass1\u 1
1.d.MoveNext()\r\n
---来自引发异常的上一个位置的堆栈结束跟踪---\r\n
Amazon.Runtime.Internal.Util.AsynchHelpers.ExclusiveSynchronizationContext.BeginMessageLoop()的
在里面
D:\\Jenkins工作区\\投石机阶段发布\\AWSDotNetPublic\\sdk\\src\\Core\\Amazon.Runtime\\Internal\\Util\\\\u mobile\\asynchHelpers.cs:line
142\r\n在
中的Amazon.Runtime.Internal.Util.asynchHelpers.RunSync[T](Func
1任务) D:\JenkinsWorkspaces\trebuchet stage release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\u mobile\asynchHelpers.cs:line 87\r\n在Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri, TimeSpan超时,IWebProxy代理)在 D:\JenkinsWorkspaces\trebuchet stage release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\AWSSDKUtils.cs:line 1008\r\n位于Amazon.Util.EC2InstanceMetadata.GetItems(字符串 RelativeOrbSolutePath,Int32尝试,布尔slurp)中的 D:\JenkinsWorkspaces\trebuchet stage release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\\u bcl+netstandard\EC2InstanceMetadata.cs:line 513\r\n在 Amazon.Util.EC2InstanceMetadata.get_IAMSecurityCredentials()在 D:\JenkinsWorkspaces\trebuchet stage release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\\u bcl+netstandard\EC2InstanceMetadata.cs:line 311\r\n在 Amazon.Runtime.DefaultInstanceProfileAWSCredentials.FetchCredentials() 在里面 D:\Jenkins Workspace\trebuchet stage release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\\u bcl+netstandard\DefaultInstanceProfileAWSCredentials.cs:line 142\r\n在 Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentials() 在里面 D:\Jenkins Workspace\trebuchet stage release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\\u bcl+netstandard\DefaultInstanceProfileAWSCredentials.cs:line 88\r\n在 Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentialsAsync() 在里面 D:\Jenkins Workspace\trebuchet stage release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\\u bcl+netstandard\DefaultInstanceProfileAWSCredentials.cs:line 106\r\n在 Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext 执行上下文)在 D:\Jenkins工作区\trebuchet阶段发布\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\credentialsreever.cs:line 90\r\n在 Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)\r\n位于 Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext 执行上下文)在 D:\JenkinsWorkspace\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英寸字符串


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



..

..

..


取消cts(CancellationTokenSource)时引发的错误。 当需要时,cts被取消

启动对的请求的HttpClient。超时设置为5秒

结论:此请求在5秒钟内没有时间执行。可能是AWS Cognito暂时存在问题或某些网络延迟问题

建议:

  • 如果该故障是暂时性的,并且可能在短时间延迟后自行纠正,则应用该故障是有意义的

  • 尝试研究网络延迟


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

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

所以我改变了这个:

this.SecClient = new AmazonCognitoIdentityProviderClient(Amazon.RegionEndpoint.EUCentral1);
变成这样

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

这似乎解决了问题!

在我的例子中,.NET Cognito客户端抛出了OperationCancelledException或SocketException(主机关闭)

因为您没有向AmazonCongnitoIdentityProviderClient的构造函数提供任何AWS凭据,所以它尝试发送HTTP请求以获取EC2实例元数据(最终尝试获取EC2实例概要文件或它应该承担的类似IAM角色)

解决方案是向构造函数提供无效的AWS凭据。 新的AmazonCongnitoIdentityProviderClient(新的Amazon.Runtime.BasicAWSCredentials(@“无效”,“凭据”))

插座例外:Hos