Injection:
PublicIServiceProvider configureservices (iservicecollection services) {//Services. Addjwtauthorization (Configuration); varTypes =New[] {typeof(Applicationmodule)}; Services. addscoped<iidentitymanager, identitymanager>(); Services. Addauthentication (Cookieauthenticationdefaults.authenticationscheme). Addcookie (Options={options. Loginpath=NewPathString ("/login/index"); Options. Accessdeniedpath=NewPathString ("/error/index"); }); Services. Addmvc (); varIService = Services. Addframework<livefactorydbcontext> (opt ={opt. Usesqlserver (Configuration.getconnectionstring ("defaultconnection")); }, types, types); returnIService; }
New corresponding class
usingMicrosoft.AspNetCore.Http;usingMicrosoft.AspNetCore.Identity;usingMicrosoft.Extensions.Logging;usingMicrosoft.Extensions.Options;usingSystem;usingSystem.Collections.Generic;usingSystem.Security.Claims;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingMicrosoft.AspNetCore.Authentication;usingMicrosoft.AspNetCore.Authentication.Cookies;usingSystem.Linq;namespacelivefactory.core{ Public InterfaceIidentitymanager {claimsidentity createidentity (identityuser user); Task Signinasync (identityuser user); Task Signoutasync (); } Public classIdentitymanager:iidentitymanager {ihttpcontextaccessor _contextaccessor; PublicIdentitymanager (ihttpcontextaccessor contextaccessor) {_contextaccessor=Contextaccessor; } Public Virtualclaimsidentity createidentity (identityuser user) {varIdentity =Newclaimsidentity (Cookieauthenticationdefaults.authenticationscheme); Identity. Addclaim (NewClaim (claimtypes.primarysid, user. ID)); Identity. Addclaim (NewClaim (claimtypes.name, user. UserName)); returnidentity; } Public Virtual AsyncTask signinasync (identityuser user) {await_contextaccessor.httpcontext.signinasync (Cookieauthenticationdefaults.authenticationscheme,NewClaimsPrincipal (createidentity (user))); } Public Virtual AsyncTask Signoutasync () {await_contextaccessor.httpcontext.signoutasync (); } }}
Logon logoff
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usinglivefactory.application;usingLiveFactory.Application.Base;usingLivefactory.core;usingMICROSOFT.ASPNETCORE.MVC;//using JFJT. Authorize.ijwttokenmanager;namespacelivefactory.web.controllers{ Public classLogincontroller:controller { Public ReadOnlyIuserservice _iuserservice; Public ReadOnlyIidentitymanager _identitymanager; PublicLogincontroller (Iuserservice userservice, Iidentitymanager identitymanager) {_iuserservice=UserService; _identitymanager=Identitymanager; } PublicIactionresult Index () {returnView (); } Public AsyncTask<resultdto<userdto>>Login (userdto loginmodel) {varresult =_iuserservice.login (Loginmodel); if(result.) Success) {await_identitymanager.signinasync (NewMicrosoft.AspNetCore.Identity.IdentityUser () {Id = Guid.NewGuid (). ToString (), PasswordHash = result. Data.Password.ToString (), UserName =result. Data.Account.ToString ()}); } returnresult; } PublicActionResult loginout () {//_authenticationmanager.signout ();_identitymanager.signoutasync (); returnRedirecttoaction ("Index"); } }}
Add the controller that needs to be validated
[Authorize]
Cases:
. NET core uses official Cookieauthentication authentication