AWS Cognito-尝试对无法访问的网络进行套接字操作

时间:2019-07-16 11:00:48

标签: amazon-web-services asp.net-core amazon-cognito

我正在尝试将AWS CognitoASP.Net Core 2.2应用一起使用。我有一个AWS免费帐户,并在Cognito下创建了一个用户池。

点击以下链接

http://snevsky.com/blog/dotnet-core-authentication-aws-cognito

appsettings.json

{
"AWS": {
"Region": "us-east-1",
"AWS_SECRET_KEY": "<mykey>",
"AWS_ACCESS_KEY_ID": "<secret>"
}}

Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDefaultAWSOptions(Configuration.GetAWSOptions("AWS"));
        services.AddAuthentication("Bearer")
                .AddJwtBearer(options =>
                {
                    options.Audience = "mykey";
                    options.Authority = "https://cognito-idp.us-east-1.amazonaws.com/<myuserpoolid>";
                });
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);



    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseAuthentication();
        app.UseMvc();
    }

API

    [HttpPost]
    [Route("api/register")]
    public async Task<ActionResult<string>> Register([FromBody]User user)
    {
        try
        {

            var cognito = new AmazonCognitoIdentityProviderClient(_region);

            var request = new SignUpRequest
            {
                ClientId = _clientId,
                Password = user.Password,
                Username = user.Username
            };

            var emailAttribute = new AttributeType
            {
                Name = "email",
                Value = user.Email
            };
            request.UserAttributes.Add(emailAttribute);
            //#LOE - this line throws the mentioned error
            var response = await cognito.SignUpAsync(request);

            return Ok();
        }
        catch (Exception ex)
        {
            return Ok(ex.Message);
        }
    }

但是在#LOE处,出现以下错误:-

  

尝试对不可达的网络进行套接字操作

我已将AWS_ACCESS_KEY_ID和AWS_SECRET_KEY添加为系统环境变量。

任何建议都值得赞赏。 谢谢!

0 个答案:

没有答案