受欢迎的博客标签

Using Cookie Authentication without ASP.NET Core Identity in ASP.NET Core 3.x (iaspnetcore)

Published

Table of Contents

 

This blogpost Demo How to Using Cookie Authentication without ASP.NET Core Identity in ASP.NET Core 3.x。customer come from no sql  database without ef framework。

 

Configuration

step 1. Config cookie  in startup.cs

In the Startup.ConfigureServices method, create the Authentication Middleware services

     services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(options =>
                {
                    options.LoginPath = "/Customer/LogIn";
                    options.LogoutPath = "/Customer/LogOut";
#if !DEBUG
                    options.Cookie.Domain = ".iaspnetcore.com";
                }
               );

options.Cookie.Domain = ".example.com"; should be your cookie domain
the dot . ensure that its a shared cookie in domain.

 

 step 2. Config UseAuthentication in startup.cs

In Startup.Configure, call UseAuthentication and UseAuthorization to set the HttpContext.User property and run Authorization Middleware for requests.

app.UseAuthentication();
app.UseAuthorization();

 step3. logout 

 public async Task<IActionResult> Logout()
        {
            //external authentication


         
           //and sign out from the current authentication scheme
            await _httpContextAccessor.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

            
            return RedirectToAction("Index", "Home");

        }

Use cookie authentication without ASP.NET Core Identity in ASP.NET Core 3.x