Asp.net core 2.2 Cookie verification

First, the registration service.

services.AddAuthentication(option =>
            {
                option.DefaultScheme = "Cookie";
                option.DefaultChallengeScheme = "Cookie";
                option.DefaultAuthenticateScheme = "Cookie";
                option.DefaultForbidScheme = "Cookie";
                option.DefaultSignInScheme = "Cookie";
                option.DefaultSignOutScheme = "Cookie";
            }).AddCookie("Cookie",option=>
            {
                option.LoginPath = "/login";
                option.AccessDeniedPath = "/forbidden";
            });

Second, login

Note: You need using Microsoft.AspNetCore.Authentication; to use HttpContext.SignInAsync

var claims = new List<Claim> {
                new Claim("user","admin"),
                new Claim("role","1,2,3,4,5"),
                new Claim("id","1")
            };
            await HttpContext.SignInAsync(new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookies")));

Third, check

Here you can use the User

1, User.Identity.IsAuthenticated is logged

2, User.Identity.Name Username

3, User.Claims other login or other parameters, can be read from here

 

---------other-----

var the Claim = new new the Claim ( " name " , " wallee " ); // me a lot of information in an information unit, as well as age, sex, family, etc. 
            var the Identity = new new ClaimsIdentity ( " ID " ); // many of my identity documents in one, and driver's license, ticket, accounting certificate, the computer two certificates, etc. 
            identity.AddClaim (the Claim); // add on top of that piece of information to my identity card inside 
            var Principal = new new ClaimsPrincipal (the identity); // the identity of this man as my initialization parameters, initialization of a ClaimsPrincipal on behalf of a principal. 
            HttpContext.SignInAsync (Principal); // Finally, this subject, call the extension method HttpContext of landing.

 

Guess you like

Origin www.cnblogs.com/ccxcn/p/11363948.html