Recent work encountered a demand, need in ASP. NET core to achieve a basic identity authentication, the following this article is mainly about the implementation of ASP. NET core to achieve user login verification of the minimum configuration of relevant information, the need for friends can refer to the following to see together.
Objective
This article mainly introduces to you about the ASP. NET core User login verification of the minimum configuration related content, share out for everyone to reference the study, the following words do not say, come together to see the detailed introduction:
The background is to add temporary login functionality to a project, simply verify that the user is logged in, and the minimum configuration and implementation code required is as follows.
Here's how:
Add the configuration of the authentication in the Startup configureservices () method:
Services. Addauthentication (Options =>{options. Defaultauthenticatescheme = Cookieauthenticationdefaults.authenticationscheme; Options. Defaultchallengescheme = Cookieauthenticationdefaults.authenticationscheme;}). Addcookie ();
Add authentication to the request pipeline in the Startup Configure () method:
App. Useauthentication ();
After verifying through the user name/password in the login program, generate the login Cookie and send it to the client via the following code:
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