部署 .net core api AWS 无服务器应用程序后出现 403 禁止访问/500 内部服务器错误


  • 创建了一个 .net 核心的 AWS 无服务器应用程序。
  • 认知用于身份验证。
  • 已配置用户和应用程序客户端。
  • 当我在本地运行解决方案时,它工作正常(请记住 是 http(。
  • 当我使用发布向导发布并使用邮递员 (https://myendpoint/Prod( 点击
    新 url 时,我立即得到:

    { "消息": "禁止" }

我只能猜测这与这里的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(

方法:开机自检 代理设置为:/api/登录 请求正文:

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

收益 率:

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

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

  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 Wizard几乎可以处理其他所有事情时,我只需要添加这些额外的策略。

{
"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,并仅复制上述策略中语句下的大括号之间的内容 - 显然包括大括号本身(。

    { "版本": "2012-10-17", "声明":[] }

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

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

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

最新更新