using Microsoft.AspNetCore.Authorization; using MICROSOFT.ASPNETCORE.MVC; namespace hellocore2.controllers{ [authorize] publicclass Admincontroller: Controller { public iactionresult Index () { return View ();}} }
usingMicrosoft.AspNetCore.Authentication;usingMicrosoft.AspNetCore.Authentication.Cookies;usingMICROSOFT.ASPNETCORE.MVC;usingSystem.Collections.Generic;usingSystem.Security.Claims;namespacehellocore2.controllers{ Public classAccountcontroller:controller { PublicIactionresult Login () {varClaims =NewList<claim>() { NewClaim (Claimtypes.name,"Wolf"), NewClaim (Claimtypes.role,"Admin"), }; varClaimidentity =Newclaimsidentity (Claims,cookieauthenticationdefaults.authenticationscheme); Httpcontext.signinasync (Cookieauthenticationdefaults.authenticationscheme,NewClaimsPrincipal (claimidentity)); returnOk ("Login"); } PublicIactionresult LogOut () {Httpcontext.signoutasync (cookieauthenticationdefaults.authenticationscheme ); returnOk ("LogOut"); } }}
usingMicrosoft.AspNetCore.Authentication.Cookies;usingMicrosoft.AspNetCore.Builder;usingMicrosoft.AspNetCore.Hosting;usingMicrosoft.Extensions.Configuration;usingMicrosoft.Extensions.DependencyInjection;namespacehellocore2{ Public classStartup { PublicStartup (iconfiguration configuration) {Configuration=configuration; } PublicIConfiguration Configuration {Get; } //This method gets called by the runtime. Use this method to add services to the container. Public voidconfigureservices (iservicecollection services) {//Configuration 1Services. Addauthentication (Cookieauthenticationdefaults.authenticationscheme). Addcookie (Options= { //Jump Login PageOptions. Loginpath ="/account/login"; }); Services. Addmvc (); } //This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env) {if(env. Isdevelopment ()) {app. Usebrowserlink (); App. Usedeveloperexceptionpage (); } Else{app. Useexceptionhandler ("/home/error"); } app. Usestaticfiles (); //Configuration 2app. Useauthentication (); App. USEMVC (Routes={routes. MapRoute (Name:"default", Template:"{controller=home}/{action=index}/{id?}"); }); } }}
Login successful
ASP. NET Core 2.0 cookie-based Certification implementation