net core 3.1 Authorize


net core 3.1  Authorize
1
//配置authorrize
services.AddAuthentication(b =>
            {
                b.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                b.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                b.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            }).
            AddCookie(b =>
            {
    //登陸地址
    b.LoginPath = "/login";
    //sid
    b.Cookie.Name = "My_SessionId";
    // b.Cookie.Domain = "shenniu.core.com";
    b.Cookie.Path = "/";
    b.Cookie.HttpOnly = true;
    //b.Cookie.Expiration = new TimeSpan(0, 0, 30);

    b.ExpireTimeSpan = new TimeSpan(0, 0, 30);
});

2.
Configure
//啟用身份認證,啟用后可以在Controller中使用User API
app.UseAuthentication();

3.

   string name = "羅分明";
//登陸授權
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, name));
                var indentity = new ClaimsIdentity(claims, "denglu");
var principal = new ClaimsPrincipal(indentity);
await HttpContext.SignInAsync(
    CookieAuthenticationDefaults.AuthenticationScheme,
    principal);
                //驗證是否授權成功
                if (principal.Identity.IsAuthenticated)
                {
                    return Redirect("/home/index");
                }

4.
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]

var a = User.Identity.Name;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM