403 Forbidden/500 部署 .net 核心后出现内部服务器错误 api AWS 无服务器应用程序
403 Forbidden/500 Internal Server Error after deploying .net core api AWS Serverless application
- 创建了一个 .net 核心 AWS 无服务器应用程序。
- Cognito 用于身份验证。
- 已配置用户和应用程序客户端。
- 当我运行本地解决方案时它工作正常(记住它
是 http).
当我使用发布向导发布并使用
点击新的url
邮递员(https://myendpoint/Prod)我立即得到:
{
"message": "Forbidden"
}
这里只能猜测是跟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 错误。
Postman yields: 500 内部服务器错误
正在使用 API 网关进行测试(Api 网关->资源-> /{代理+}->任意->测试->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":""}
好的 - 这可能会在某个阶段对某人有所帮助
最初的 "Forbidden" 错误实际上并不是权限问题。当通过向导部署 API 时,它实际上会在 URL 的末尾添加 "staging" 目录。我没有将此添加到我的邮递员请求中。这很容易做到和忽视。有点误导——它真的应该是 404。
第二部分(编辑 #1)500 内部服务器错误。除了针对您的 API 启用 cloudwatch 日志然后搜索之外,没有真正的 "easy" 方法来解决这个问题。
观看此 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":"*"
}
]
}
政策的创建和应用者:
- 服务->IAM->策略->创建策略->Json->
粘贴上面的政策。 (如果您收到有关格式错误的 JSON 的错误,请使用 JSON 框中现有的 JSON 并仅复制上述策略声明下大括号之间的内容 - 包括大括号本身显然)。
{
"Version": "2012-10-17",
"Statement": []
}
查看政策并完成创建
转到角色
单击记录并显示在 Cloudwatch 日志中的 AspNetCoreFunctionRole 用户
- 在权限下单击附加策略
- 输入您新创建的策略的名称
- Post 到您的登录页面,瞧
- 创建了一个 .net 核心 AWS 无服务器应用程序。
- Cognito 用于身份验证。
- 已配置用户和应用程序客户端。
- 当我运行本地解决方案时它工作正常(记住它 是 http).
当我使用发布向导发布并使用
点击新的url 邮递员(https://myendpoint/Prod)我立即得到:{ "message": "Forbidden" }
这里只能猜测是跟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 错误。
Postman yields: 500 内部服务器错误
正在使用 API 网关进行测试(Api 网关->资源-> /{代理+}->任意->测试->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":""}
好的 - 这可能会在某个阶段对某人有所帮助
最初的 "Forbidden" 错误实际上并不是权限问题。当通过向导部署 API 时,它实际上会在 URL 的末尾添加 "staging" 目录。我没有将此添加到我的邮递员请求中。这很容易做到和忽视。有点误导——它真的应该是 404。
第二部分(编辑 #1)500 内部服务器错误。除了针对您的 API 启用 cloudwatch 日志然后搜索之外,没有真正的 "easy" 方法来解决这个问题。
观看此 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":"*"
}
]
}
政策的创建和应用者:
- 服务->IAM->策略->创建策略->Json->
粘贴上面的政策。 (如果您收到有关格式错误的 JSON 的错误,请使用 JSON 框中现有的 JSON 并仅复制上述策略声明下大括号之间的内容 - 包括大括号本身显然)。
{ "Version": "2012-10-17", "Statement": [] }
查看政策并完成创建
转到角色 单击记录并显示在 Cloudwatch 日志中的 AspNetCoreFunctionRole 用户
- 在权限下单击附加策略
- 输入您新创建的策略的名称
- Post 到您的登录页面,瞧