ASP. NET Core 2.0 cookie-based Certification implementation

Source: Internet
Author: User
Tags httpcontext

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

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.