Home  >  Article  >  Backend Development  >  Sample code to implement minimum configuration for user login verification in ASP.NET Core

Sample code to implement minimum configuration for user login verification in ASP.NET Core

黄舟
黄舟Original
2017-10-11 10:22:141661browse

I recently encountered a requirement at work, which requires implementing a basic identity authentication in ASP.NET Core. The following article mainly introduces you to the minimum configuration for implementing user login verification in ASP.NET Core. Friends in need can refer to the information. Let’s take a look together.

Preface

This article mainly introduces the relevant content about the minimum configuration of ASP.NET Core user login verification, and shares it with you. Reference study, not much to say below, let’s take a look at the detailed introduction:

The background is to add a temporary login function to a project. You only need to verify whether the user is logged in. The minimum configuration required is the same as The implementation code is as follows.

The method is as follows:

Add the Authentication configuration in 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 verifying the username/password in the login program, pass The following code generates a login cookie and sends it 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);

Summary

The above is the detailed content of Sample code to implement minimum configuration for user login verification in ASP.NET Core. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn