403 Forbidden/500 Internal Server Error after deploying .net core api AWS serverless application


The_Chud
  • Created a .net core AWS serverless application.
  • Cognito is used for authentication.
  • The user and application clients have been configured.
  • When I run the solution locally it works fine (remember it's http).
  • When I publish using the publish wizard and hit the new url using
    postman ( https://myendpoint/Prod ) I immediately get:

    {"message":"forbidden"}

I can only guess it has something to do with http/https here.

Authentication Controller:

 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);

        }
    }

start.config service

 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
                    };
                });

EDIT #1 It looks like I fixed the forbidden msg, but am now getting a 500 error.

Postman produces: 500 Internal Server Error

Use API Gateway for testing (Api Gateway->Resources-> /{proxy+}->Any->Test->Post)

Method: POST proxy set to: /api/signin Request body:

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

Yield:

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

ok - this might help someone at some stage

  1. The original "forbidden" error wasn't actually a permissions issue. When you deploy the API through the wizard, it actually adds a "staging" directory to the end of the URL. I didn't add this to my postman request. It's simple to do and ignore. This is a bit misleading - it really should be a 404.

  2. Second part (edit #1) 500 Internal Server Error. There is no real "easy" way to fix this other than enabling cloudwatch logs for your API and then searching.

Follow this YouTube video to learn how to set it up : https://www.youtube.com/watch?v=R67huNjk88w

After looking at the logs, I found that it was a permissions issue:

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 ---> 

Credit goes to the following articles:

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

Step 2.2.4.4 to be specific. Since I found that the Visual Studio wizards can handle pretty much everything else, I just had to add these extra policies.

{
   "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":"*"
      }
   ]
}

Policies are created and applied by:

  1. Service->IAM->Policies->Create Policy->Json->
  2. Paste the strategy above. (If you get an error about malformed JSON, use the existing JSON in the JSON box and just copy the content between the curly braces under Statement in the above strategy - including the curly braces themselves, obviously).

    {"version":"2012-10-17","statement":[]}

  3. Go to view policy and finish creating

  4. Go to role click on AspNetCoreFunctionRole user logged and shown in Cloudwatch log

  5. Under Permissions click Attach Policy
  6. Enter the name of your newly created policy
  7. Publish to your login page and voila

Related


Server error after deploying mvc application to aws

Craig PH I have an asp.net MVC application that allows users to upload PDFs to the system (PDF files are saved to a folder called Surveys). The PDF is then sent to the Azure Read API and the returned data is stored in a .text file on the system (so I can enter

asp.net core application. 500 Internal Server Error

David Edel I uploaded my website to godaddy windows hosted with plesk. All files are uploaded but I get an error: 500 Internal Server Error. There is a problem with the resource you are looking for and cannot be displayed. By researching the problem online, I

ASP.NET Core Web API 500 Internal Server Error

Fear I am trying to build an API within the ASP.NET Core framework. I made a basic controller that just returns a string: namespace Dojo_Api.Controllers.Forum { //[Authorize] [Route("api/[controller")] public class ForumController : Controller

AWS Serverless 403 Forbidden API Gateway

Abi Gulala I am trying to create a serverless Nodejs application. I deployed the app to S3 and it's working. But the url seems to be https://xxxxxxxx.xxxxx-api.us-west-2.amazonaws.com/prod . So whenever my app tries to get a css file or a video file or even a

AWS Serverless 403 Forbidden API Gateway

Abi Gulala I am trying to create a serverless Nodejs application. I deployed the app to S3 and it's working. But the url seems to be https://xxxxxxxx.xxxxx-api.us-west-2.amazonaws.com/prod . So whenever my app tries to get a css file or a video file or even a

AWS Serverless 403 Forbidden API Gateway

Abi Gulala I am trying to create a serverless Nodejs application. I deployed the app to S3 and it's working. But the url seems to be https://xxxxxxxx.xxxxx-api.us-west-2.amazonaws.com/prod . So whenever my app tries to get a css file or a video file or even a

AWS Serverless 403 Forbidden API Gateway

Abi Gulala I am trying to create a serverless Nodejs application. I deployed the app to S3 and it's working. But the url seems to be https://xxxxxxxx.xxxxx-api.us-west-2.amazonaws.com/prod . So whenever my app tries to get a css file or a video file or even a

AWS Serverless 403 Forbidden API Gateway

Abi Gulala I am trying to create a serverless Nodejs application. I deployed the app to S3 and it's working. But the url seems to be https://xxxxxxxx.xxxxx-api.us-west-2.amazonaws.com/prod . So whenever my app tries to get a css file or a video file or even a

Internal server error when deploying express using serverless

no others **[Nothing like stupid or useless questions we all learn here]** I'm facing a problem that I really can't solve for days, I'm trying to use serverless to get my express app The program deploys to aws but when i try to access the endpoint i get this e

Error deploying .Net Core 2.0 Web API to AWS Elastic Beanstalk

0014 I have built my Web API using .Net Core 2.0 . Now I'm trying to deploy the project to AWS using its Elastic Beanstalk service . I also used Visual Studios AWS deployment tools . During deployment I get the following error; An error occurred during deploym

Error deploying .Net Core 2.0 Web API to AWS Elastic Beanstalk

0014 I have built my Web API using .Net Core 2.0 . Now I'm trying to deploy the project to AWS using its Elastic Beanstalk service . I also used Visual Studios AWS deployment tools . During deployment I get the following error; An error occurred during deploym

Error deploying .Net Core 2.0 Web API to AWS Elastic Beanstalk

0014 I have built my Web API using .Net Core 2.0 . Now I'm trying to deploy the project to AWS using its Elastic Beanstalk service . I also used Visual Studios AWS deployment tools . During deployment I get the following error; An error occurred during deploym

500 server error when deploying Laravel application in aws

Abbie I've been trying to fix this bug for 2 weeks now and it's still not resolved. I've used all the answers related to this question but haven't found any solution that works for my problem :( used command chmod -R o+w laravel_blog/storage sudo chmod -R 755

Web application using c#.net Core internal server error 500

alone My app didn't work until 2 hours ago. In postman, my server is called and responds fine. I was notified about upgrading VS2019, so I did. Now every time I send a request to the server from postman, I get this error: I tried to downgrade to .net 3.0.0 but

Deploying Website: 500 - Internal Server Error

Jacob Jedricek I am trying to deploy an ASP.NET application. I have deployed the site to IIS, but when accessing it with a browser it shows me this: Server Error 500 Internal Server Error. There is a problem with the resource you are looking for and cannot be

Deploying Website: 500 - Internal Server Error

Jacob Jedricek I am trying to deploy an ASP.NET application. I have deployed the site to IIS, but when accessing it with a browser it shows me this: Server Error 500 Internal Server Error. There is a problem with the resource you are looking for and cannot be

Deploying Website: 500 - Internal Server Error

Jacob Jedricek I am trying to deploy an ASP.NET application. I have deployed the site to IIS, but when accessing it with a browser it shows me this: Server Error 500 Internal Server Error. There is a problem with the resource you are looking for and cannot be

Deploying ASP.NET Core API to local IIS error 500

Michael Reno Platama Setavan I have a problem with my application after deploying to local IIS. I've made an API using ASP.NET Core and have run it on Visual Studio and it works. It can perform GET, POST, PUT and DELETE requests. Then I want to deploy it to lo

Deploying ASP.NET Core API to local IIS error 500

Michael Reno Platama Setavan I have a problem with my application after deploying to local IIS. I've made an API using ASP.NET Core and have run it on Visual Studio and it works. It can perform GET, POST, PUT and DELETE requests. Then I want to deploy it to lo