gpt4 book ai didi

AWS API Gateway 自定义授权 Lambda 的 C# 实现

转载 作者:太空狗 更新时间:2023-10-30 00:49:01 25 4
gpt4 key购买 nike

我有一个关于使用 C# 编码的 lambda 对 AWS API Gateway 进行自定义授权的问题。在 AWS Lambdas 的文档中,函数签名如下:

returnType handler-name(inputType input, ILambdaContext context) {
...
}

需要为函数处理程序指定 inputType 和 returnType。 API Gateway中的自定义授权,inputType和returnTypes应该是什么?提前致谢。

最佳答案

您可以选择强类型方法,而无需发明需要遵循所需架构的自定义类。

使用 Nuget 包:

Amazon.Lambda.APIGatewayEvents

输入模式:

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-input.html

输出模式:

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html

您的函数原型(prototype)可以类似于:

using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;

public class Function
{
public APIGatewayCustomAuthorizerResponse FunctionHandler(APIGatewayCustomAuthorizerRequest input, ILambdaContext context)
{
bool ok = false;
// authorization logic here...
if(input.AuthorizationToken == "up-down-left-right-a-b-select-start")
{
ok = true;
}
return new APIGatewayCustomAuthorizerResponse
{
PrincipalID = "***",//principal info here...
UsageIdentifierKey = "***",//usage identifier here (optional)
PolicyDocument = new APIGatewayCustomAuthorizerPolicy
{
Version = "2012-10-17",
Statement = new List<APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement>() {
new APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement
{
Action = new HashSet<string>(){"execute-api:Invoke"},
Effect = ok ? "Allow" : "Deny",
Resource = new HashSet<string>(){ "***" } // resource arn here
}
},
}
};
}
}

关于AWS API Gateway 自定义授权 Lambda 的 C# 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41451582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com