【问题标题】:403 Forbidden/500 Internal Server Error after deploying .net core api AWS Serverless application部署 .net core api AWS 无服务器应用程序后出现 403 Forbidden/500 Internal Server Error
【发布时间】:2020-04-18 11:58:39
【问题描述】:
  • 创建了一个 .net core AWS 无服务器应用程序。
  • Cognito 用于身份验证。
  • 已配置用户和应用客户端。
  • 当我在本地运行解决方案时,它运行良好(请记住 是 http)。
  • 当我使用发布向导发布并点击新网址时
    邮递员 (https://myendpoint/Prod) 我立即得到:

    { “消息”:“禁止” }

我这里只能猜测是和http/https有关。

身份验证控制器:

 public class AuthenticationController : Controller
    {
        [HttpPost]
        [Route("api/signin")]
        public async Task<ActionResult<string>> SignIn(User user)
        {
            var cognito = new AmazonCognitoIdentityProviderClient(RegionEndpoint.APSoutheast2);

            var request = new AdminInitiateAuthRequest
            {
                UserPoolId = "ap-southeast-2_MYPOOLID",
                ClientId = "MYCLIENTID",
                AuthFlow = AuthFlowType.ADMIN_USER_PASSWORD_AUTH
            };

            request.AuthParameters.Add("USERNAME", user.Username);
            request.AuthParameters.Add("PASSWORD", user.Password);

            var response = await cognito.AdminInitiateAuthAsync(request);
            return Ok(response.AuthenticationResult);

        }
    }

Startup.ConfigureServices

 services.AddSingleton<IAuthorizationHandler, CognitoGroupAuthorisationHandler>();
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidIssuer = "https://cognito-idp.ap-southeast-2.amazonaws.com/ap-southeast-2_MYPOOL",
                        ValidateIssuerSigningKey = true,
                        ValidateIssuer = true,
                        ValidateLifetime = true,
                        ValidAudience = "MYKEY",
                        ValidateAudience = true
                    };
                });

编辑 #1 看来我解决了禁止的消息,但现在收到 500 错误。

邮递员产生:500 内部服务器错误

使用 API Gateway 进行测试(Api Gateway->Resources-> /{proxy+}->Any->Test->Post)

方法:POST 代理设置为:/api/signin 请求正文:

{
    "username": "xxx",
    "password":"yyy"
}

产量:

{"Strict-Transport-Security":"max-age=2592000","ErrorType":"AmazonCognitoIdentityProviderException","X-Amzn-Trace-Id":"Root=xxxxx;Sampled=0","Content-Type":""}

【问题讨论】:

    标签: c# .net amazon-cognito aws-sdk-net


    【解决方案1】:

    好的 - 这可能会在某个阶段对某人有所帮助

    1. 最初的“禁止”错误实际上并不是权限问题。当通过向导部署 API 时,它实际上在 URL 的末尾添加了“暂存”目录。我没有将此添加到我的邮递员请求中。这很容易做和忽略。这有点误导 - 它应该真的是 404。

    2. 第二部分(编辑 #1)500 内部服务器错误。除了针对您的 API 启用 cloudwatch 日志然后进行搜索之外,没有真正“简单”的方法可以解决此问题。

    观看此 YouTube 视频,了解如何设置: https://www.youtube.com/watch?v=R67huNjk88w

    查看日志后发现是权限问题:

    Amazon.CognitoIdentityProvider.AmazonCognitoIdentityProviderException: User: arn:aws:sts::xxxxx:assumed-role/xxx-AspNetCoreFunctionRole-xxx/xxx-AspNetCoreFunction-xxxx is not authorized to perform: cognito-idp:AdminInitiateAuth on resource: arn:aws:cognito-idp:ap-southeast-2:xxxx:userpool/ap-southeast-2_xxxxx ---> 
    

    归功于以下文章:

    https://medium.com/@fcavalcantirj/tutorial-aws-api-gateway-cognito-userpool-8cc5838eac0

    具体步骤 2.2.4.4。因为我发现 Visual Studio 向导几乎可以处理所有其他事情,所以我只需要添加这些额外的策略。

    {
       "Version":"2012–10–17",
       "Statement":[
          {
             "Effect":"Allow",
             "Action":[
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
             ],
             "Resource":"arn:aws:logs:*:*:*"
          },
          {
             "Effect":"Allow",
             "Action":[
                "cognito-identity:*",
                "cognito-idp:*",
                "cognito-sync:*",
                "iam:ListRoles",
                "iam:ListOpenIdConnectProviders",
                "sns:ListPratformApplications"
             ],
             "Resource":"*"
          }
       ]
    }
    

    政策由以下人创建和应用:

    1. 服务->IAM->策略->创建策略->Json->
    2. 粘贴上面的政策。 (如果您收到有关格式错误的 JSON 的错误,请使用 JSON 框中的现有 JSON,并仅复制上述策略中 Statement 下的大括号之间的内容 - 显然包括大括号本身)。

      { “版本”:“2012-10-17”, “陈述”: [] }

    3. 转到查看策略并完成创建

    4. 转到角色 单击已记录并显示在 Cloudwatch 日志中的 AspNetCoreFunctionRole 用户

    5. 在权限下单击附加策略
    6. 输入新创建策略的名称
    7. 发布到您的登录页面,然后瞧

    【讨论】:

      猜你喜欢
      • 2020-05-19
      • 1970-01-01
      • 2020-10-12
      • 2017-02-20
      • 2021-12-04
      • 2022-09-27
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多