這篇文章將為大家詳細講解有關(guān)Asp.Net Core 中怎么利用Cookie實現(xiàn)身份認證,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)與策劃設(shè)計,綏德網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:綏德等地區(qū)。綏德做網(wǎng)站價格咨詢:18980820575
一 注冊Cookie認證服務(wù) ConfigureServices
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(o=> { o.LoginPath = new PathString("/Home/Index"); o.LogoutPath = new PathString("/Account/Login"); } );
二 配置中間件 Configure
app.UseAuthentication(); //添加授權(quán)中間件 必須卸載app.UseMvc();之前。
三 登錄
var claims = new[] { new Claim("UserName","AESCR"), new Claim("Sex","男") }; var claimsIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme); claimsIdentity.AddClaim(new Claim(ClaimTypes.Name, "AESCR")); claimsIdentity.AddClaim(new Claim("密碼","6666")); ClaimsPrincipal user = new ClaimsPrincipal(claimsIdentity); HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user,new AuthenticationProperties() { IsPersistent = true, AllowRefresh = true, RedirectUri = "/Home/Index", }).Wait();
四 讀取cookie
if (context.HttpContext.User.Identity.IsAuthenticated){ var userName = context.HttpContext.User.FindFirst(ClaimTypes.Name).Value; }
五 退出
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
六 其他
[AllowAnonymous] [Authorize] [Authorize(Roles = "Admin,IBusiness,IApproval")]....
關(guān)于Asp.Net Core 中怎么利用Cookie實現(xiàn)身份認證就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
本文名稱:Asp.NetCore中怎么利用Cookie實現(xiàn)身份認證
文章URL:http://aaarwkj.com/article20/ihpdjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、App開發(fā)、面包屑導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)