日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

[转]Asp.Net Core 简单的使用加密的Cookie保存用户状态

發(fā)布時間:2025/3/20 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转]Asp.Net Core 简单的使用加密的Cookie保存用户状态 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文轉(zhuǎn)自:http://www.cnblogs.com/Joes/p/6023820.html

在以前的Asp.Net中可以用?FormsAuthentication 類的一系列方法來使用加密的Cookie存儲用戶身份,使用簡單,可控性強。在Asp.Net Core中是否也可以的?答案是當(dāng)然的。

?

使用步驟:

1、在?project.json 中添加項目依賴?"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0":

"dependencies": {"Microsoft.NETCore.App": { "version": "1.0.1", "type": "platform" }, "Microsoft.AspNetCore.Diagnostics": "1.0.0", "Microsoft.AspNetCore.Mvc": "1.0.1", "Microsoft.AspNetCore.Razor.Tools": { "version": "1.0.0-preview2-final", "type": "build" }, "Microsoft.AspNetCore.Routing": "1.0.1", "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.1", "Microsoft.AspNetCore.StaticFiles": "1.0.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", "Microsoft.Extensions.Configuration.Json": "1.0.0", "Microsoft.Extensions.Logging": "1.0.0", "Microsoft.Extensions.Logging.Console": "1.0.0", "Microsoft.Extensions.Logging.Debug": "1.0.0", "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0", "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0", "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0" }

2、在?Startup.cs 中添加中間件:

     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = "member",  // Cookie 驗證方案名稱,后面多處都需要用到,部分地方必須要求常量,所以直接配置為字符串。 AutomaticAuthenticate = true,     // 是否自動啟用驗證,如果不啟用,則即便客服端傳輸了Cookie信息,服務(wù)端也不會主動解析。                               // 除了明確配置了 [Authorize(ActiveAuthenticationSchemes = "上面的方案名")] 屬性的地方,才會解析,此功能一般用在需要在同一應(yīng)用中啟用多種驗證方案的時候。比如分Area. LoginPath = "/account/login"     // 登錄頁 }); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }

3、創(chuàng)建一個新的?Controller,并添加登錄的方法,大致如下:

     [AllowAnonymous]public async Task<IActionResult> Login(){// 這里應(yīng)該寫業(yè)務(wù)邏輯來獲取用戶名,用戶Id等信息。 var userId = 10000; var userName = "admin"; // ======================== var identity = new ClaimsIdentity("Forms"); // 指定身份認(rèn)證類型 identity.AddClaim(new Claim(ClaimTypes.Sid, userId.ToString()));  // 用戶Id identity.AddClaim(new Claim(ClaimTypes.Name, userName));       // 用戶名稱 var principal = new ClaimsPrincipal(identity); await HttpContext.Authentication.SignInAsync("member", principal, new AuthenticationProperties { IsPersistent = true });
       string returnUrl = Request.Query["returnUrl"];
       if (!string.IsNullOrEmpty(returnUrl)) return Redirect(returnUrl);
       return RedirectToAction("index", "account");
}

4、退出登錄方法:

public async Task<IActionResult> Logout(){await HttpContext.Authentication.SignOutAsync("member");   // Startup.cs中配置的驗證方案名        return RedirectToAction("index", "home");      }

5、獲取用戶信息:

[Authorize(ActiveAuthenticationSchemes = "member")]public IActionResult Index() { var userId = User.FindFirst(ClaimTypes.Sid).Value; var userName = User.Identity.Name; return Json(new { Id = userId, Name = userName }); }

?

?

其它說明:

這里生成的Cookie是加密過的,大概如下:

相關(guān)的Cookie名稱,域,過期時間等都可以在?Startup.cs 中進行設(shè)置,大概如下:

app.UseCookieAuthentication(new CookieAuthenticationOptions{AuthenticationScheme = "member", // 驗證方案名 AutomaticAuthenticate = true, // 是否自動啟用驗證 LoginPath = "/account/login", // 登錄地址 CookieDomain = "abc.com", // 驗證域 CookieName = "abc", // Cookie 名稱 CookiePath = "/", // Cookie 路徑 ExpireTimeSpan = TimeSpan.FromDays(3), // 過期時間 SlidingExpiration = true, // 是否在過期時間過半的時候,自動延期 CookieHttpOnly = true // 是否允許客戶端Js獲取。默認(rèn)True,不允許。 });

?

步驟很簡單,也是極好用的,若結(jié)合是否自動啟用驗證的AutomaticAuthenticate來進行Area分區(qū)域認(rèn)證,靈活性更強。

?

總結(jié)

以上是生活随笔為你收集整理的[转]Asp.Net Core 简单的使用加密的Cookie保存用户状态的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。