How to implement AWS Cognito Authentication in ASP.NET Core 5.0 Web Application
Published: 2021-05-10 09:28:55

---
title: How to implement AWS Cognito Authentication in ASP.NET Core 5.0 Web Application
published: true
date: 2021-04-21 05:00:00 UTC
tags: 
canonical_url: https://www.faciletechnolab.com/blog/how-to-implement-aws-cognito-authentication-in-aspnet-core-50-web-application/
---

In this article, I am going to show you how to implement AWS Cognito Authentication in ASP.NET Core 5.0 Web application. I will create ASP.NET Core 5.0 project and show you step by step how to use it for authentication and authorization against AWS Cognito Authentication.

 ![how to implement AWS Cognito Authentication in ASP.NET Core 5.0 Web application](https://www.faciletechnolab.com/media/wgjfztjg/cognito-cover.png "how to implement AWS Cognito Authentication in ASP.NET Core 5.0 Web application")

### Prerequisites

Before you start to follow steps given in this article, you will need an AWS Account, and Visual Studio 2019 with .NET 5.0 development environment step.

 

### Setting up AWS Cognito User Pool

Login to your AWS account and go to Cognito. Click on Create User Pool. In the Create a user pool wizard, enter pool name and click on Step through settings.

![](https://www.faciletechnolab.com/media/lanjlrx1/cognito-1.png?rmode=max&width=500)

In next step, you will see “How do you want your end user to sign in?” step, where I chose Email address or phone number and in that section choose Allow email address so that users can only login through their email and there is no need to create separate username.

In the “Which standard attributes do you want to require” section, I chose email, family\_name, given\_name and name attributes as required attributes so that once user is logged in and redirected back to ASP.NET, our application read these claims to do various business operations. Click next once you are done with the selections to go to next step.

![](https://www.faciletechnolab.com/media/2jcfbzxi/cognito-2.png?rmode=max&width=500)

In the next step, What password strength do you want to require? and Do you want to allow users to sign themselves up?, I kept default settings as is, and in section How quickly the temporary password set by administrator expire if not used?, I change the Days to expire to 30 to make sure user has enough time to accept the invite.   
  
Once you are done updating, click on next step.

![](https://www.faciletechnolab.com/media/xusmdtws/cognito-3.png?rmode=max&width=500)

In the Multi-factor authentication step, I kept all the settings as is and click on next step

![](https://www.faciletechnolab.com/media/eo0dpk4g/cognito-4.png?rmode=max&width=500)

in the next step Do you want to remember your users device? I changed it to always and clicked on next step.

![](https://www.faciletechnolab.com/media/efspw32s/cognito-5.png?rmode=max&width=500)

In the next step Which app client will have access to this user pool?, I clicked on add client and configured it as per the below settings and clicked on create app client button at the bottom.

![](https://www.faciletechnolab.com/media/x1jnxidi/cognito-7.png?rmode=max&width=500)

In the next step Which app client will have access to this user pool?, I clicked on add client and configured it as per the below settings and clicked on create app client button at the bottom.

![](https://www.faciletechnolab.com/media/wdjnliay/cognito-8.png?rmode=max&width=500)

Once you click next you will see option to review the configuration of the user pool that you are creating and an option to create it at the bottom. Click on create pool.

![](https://www.faciletechnolab.com/media/ljvojxho/cognito-9.png?rmode=max&width=500)

 

### Creating ASP.NET Core 5.0 web application

Open visual studio and click on Create a new project in the right and select “Asp.net core web app” as shown in below image and click next.

![create new project in visual studio](https://www.faciletechnolab.com/media/ktehhhtu/azure-ad-1.png?rmode=max&width=500)

In the configure your new project section enter name and location of your project as shown in below image and click next

![configure your new project in visual studio](https://www.faciletechnolab.com/media/c44bb0r2/jwt-2.png?rmode=max&width=500)

In the additional information step, select .NET 5.0 in the target framework, Authentication Type to none and check Configure HTTPS checkbox and click on create.

![configure Additional information for new project in visual studio](https://www.faciletechnolab.com/media/51tftyl0/jwt-3.png?rmode=max&width=500)

Once the project is created, run the project and copy the url of the project from the browser. We will need this url in the Azure AD app registration and setup.

 

### Setting up AWS Cognito options

Now, switch back to AWS Cogntio and click on general settings and copy Pool Id, we will need it in the web application configuration setting.

![](https://www.faciletechnolab.com/media/3jajzggl/cognito-10.png?rmode=max&width=500)

Then go to App Clients section and copy App client Id. We will also need it in the web app configuration

![](https://www.faciletechnolab.com/media/wg3nwh2f/cognito-11.png?rmode=max&width=500)

ext, click on App client settings As shown in below image, enter `/signin-oidc` in the call back url(s), in the sign out url(s), enter ``. In Oauth 2.0 configuration section, check n Implicit grant and in Allowed Oauth Scopes, select email, profile and openId checkboxes.

![](https://www.faciletechnolab.com/media/fc3fto14/cognito-14.png?rmode=max&width=500)

 

### Setup AWS Authentication in ASP.NET Core 5.0 Web Application

Finally, switch back to visual studio and open appsettings.development.json and enter add below configuration

<script src="https://gist.github.com/plakhlani/06b63efb6929568aa84201ad8cfcc9c0.js"></script>

Now, replace ``, `your-region-id` and `your-user-pool-id` to actual values copied from AWS Cognito user pool as per the documentation in the previous seciton.

Next, add package manager console and add following two package references to your web application.

```
Install-package Microsoft.AspNetCore.Authentication.OpenIdConnect
install-package Microsoft.Identity.Web
```

Next, open startup.cs in your project and paste following code in the `ConfigureServices` method after `services.AddRazorPages();` line.

<script src="https://gist.github.com/plakhlani/2d2a081fc5d67d5b8fcd5783b036c9dd.js"></script>

Next, in the `Startup.cs`, go to `Configure` method and add `app.UseAuthentication();` line before `app.UseAuthorization();` line.

Next, open index.cshtml in the views folder and enter following lines of code

<script src="https://gist.github.com/FacileTechnolab/8081e4bdbb2db8e8fffea41df6bb2d56.js"></script>

Switch to /Pages/Index.cshtml.cs and add `[Authorize]` attribute to the IndexModel class.

Now open Pages/Index.cshtml and add following code to the end of the file.

<script src="https://gist.github.com/plakhlani/0946922f0b2d7935b2c846716b45d6cc.js"></script>

Save all files and run your project.

You will notice that once you run the project, the browser will redirect to AWS Cognito login page and upon return, you will see your email address on the page.

 

### Conclusion

We just learned how to implement a basic AWS Cognito authentication in .NET 5.0.