Minimum configuration sample code for user login authentication in ASP. NET Core

  • 2021-10-13 07:09:19
  • OfStack

Preface

This article mainly introduces the relevant contents about the minimum configuration of ASP. NET Core user login authentication, and shares it for your reference and study. The following words are not much to say, let's take a look at the detailed introduction:

Background is to add temporary login function in a project, only need to verify whether the user login, the required minimum configuration and implementation code is as follows.

The method is as follows:

Add the configuration of Authentication to the ConfigureServices () method of Startup:


services.AddAuthentication(options =>
{
 options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
 options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie();

Add Authentication to the request pipeline in the Configure () method of Startup:


app.UseAuthentication();

After the user name/password is verified in the login program, the login Cookie is generated by the following code and sent to the client:


var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, model.Email) }, "Basic");
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
 claimsPrincipal);

Summarize


Related articles: