In this article, we will learn how to implement Cookie Authentication in ASP.NET Core without using Identity. I will try to cover every step so that beginners can easily understand.
This article is the part of ASP.NET Core learning series. If you are new to ASP.NET Core, then I will recommend you to go through the previous articles of this series,
Authentication is the process of verifying the identity of the user. In this article, we will authenticate the user by verifying the user’s credentials. I am using Visual Studio 2019 to demonstrate this example.
Let’s implement the Cookie Authentication in ASP.NET Core step by step
Open the Visual Studio and click on Create a new Project.

Select ASP.NET Core Empty project and click on next.

Give a name to your Project, select the location for the project creation, and click on Next.

Select the Target Framework. I am using .Net Core 3.1 (LTS) and click on create.

A similar kind of project as shown in the below image will be created.

As we have created an empty project, let's open the Startup.cs file and add the highlighted code. In the ConfigureServices method, I have added the AddControllersWithViews service and added the UseEndpoints middleware in Configure method as shown in the below image.

Add a Controllers folder in the project in which we will create all the controllers.

Let’s add a HomeController by right-clicking on the Controller folder. Go to Add and click on Controller. Select the empty controller as shown in the below image.


Right-click on the project and click on manage NuGet Packages. Browse Bootstrap (only required for the UI Style) and click on Install.

In order to serve the static files like js, CSS, images, etc., we need to UseStaticFiles middleware in Configure method.

I have added to Action methods in HomeController i.e., Index and ConfidentialData.

So, on clicking on the Home and confidential Data link in the navigation the below screen will be shown.


Another controller i.e., AccountController with a Login page as shown in the below image.

In ConfigureServices method of Startup.cs, create an Authentication Middleware Services with the AddAuthentication and AddCookie method. Authentication scheme passed to AddAuthentication sets to the default authentication scheme for the app. CookieAuthenticationDefaults.AuthenticationScheme provides “Cookies” for the scheme. In AddCookie extension method, set the LoginPath property of CookieAuthenticationOptions to “/account/login”. CookieAuthenticationOptions class is used to configure the authentication provider options.

In Configure method of Startup.cs, call UseAuthentication and UseAuthorization method before calling the endpoints.

Now let’s add the Authorize attribute, on ConfidentialData action method. Now only authenticate methods can access that ActionMethod.

Let’s click on Confidential Data link in the navbar. It will redirect to “Account/Login” page as the user is not authenticated yet.

In AccountController, the Login action method receives the return URL as a parameter to which the user needs to be redirected after the successful authentication. Below is the Login.cshtml code used in the example.
- @model CookieAuthentication.Models.LoginModel
- @{
- ViewData["Title"] = "Login";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h2>Login</h2>
- <hr />
- <div class="row">
- <div class="col-md-4">
- <form asp-action="Login">
- <div asp-validation-summary="ModelOnly" class="text-danger"></div>
- @if (!string.IsNullOrEmpty(ViewBag.Message))
- {
- <span class="text-danger">
- @ViewBag.Message
- </span>
- }
- @Html.HiddenFor(x => x.ReturnUrl)
- <div class="form-group">
- <label asp-for="UserName" class="control-label"></label>
- <input asp-for="UserName" class="form-control" />
- <span asp-validation-for="UserName" class="text-danger"></span>
- </div>
- <div class="form-group">
- <label asp-for="Password" class="control-label"></label>
- <input asp-for="Password" class="form-control" />
- <span asp-validation-for="Password" class="text-danger"></span>
- </div>
- <div class="form-group">
- <div class="checkbox">
- <label>
- <input asp-for="RememberLogin" /> @Html.DisplayNameFor(model => model.RememberLogin)
- </label>
- </div>
- </div>
- <div class="form-group">
- <input type="submit" value="Login" class="btn btn-default" />
- </div>
- </form>
- </div>
- </div>





Mark TaborPosted Jul 25, 2024, 6:09 PM
Hi Anoop I download and check the code it works but I found it did not logout the existing user when someone else tried to login with same credentials using another device can you help how to achieve this ?
Michal SodomkaPosted Feb 2, 2023, 1:16 PM
Thank you for this great article
Marwen marwenPosted May 12, 2022, 12:33 AM
I was wondering haw can i get the claims value..?
Mali AnalitikPosted Feb 16, 2022, 8:08 PM
Great document! I have a question. How can I set the expire time out? Now, if i wait 2-3 minutes after login, it logs out. Thank you.
Avinash KunnurePosted Jan 2, 2022, 2:36 AM
Thank you great article
Rajesh ThampiPosted Jun 11, 2021, 6:58 AM
Thanks Anoop, was trying to find an alternative for "Forms Authentication" and glad to find a sample here.