. NET core uses official Cookieauthentication authentication

Source: Internet
Author: User
Tags httpcontext

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.