咱們知道在Cookie中有些字符是特殊字符,這些字符是不能出如今Cookie的鍵值中的。javascript
好比"="是Cookie中用來分隔鍵和值的特殊字符,例如:Key01=Value01,表示的是一個Cookie鍵值對,用"="分隔鍵和值。html
好比";"是Cookie中用來分隔多個Cookie鍵值的特殊字符,例如:Key01=Value01;Key02=Value02,表示的是兩個Cookie鍵值對,用";"分隔兩個Cookie鍵值。java
那麼若是咱們在ASP.NET Core中,將這些特殊字符寫入Cookie的鍵值中,會不會發生錯誤呢?jquery
爲此咱們來作一個實驗,首先咱們建立一個ASP.NET Core MVC 3.0項目:AspNetCoreCookieTestweb
在這個項目的HomeController中,咱們建立了四個Action方法:瀏覽器
HomeController的代碼以下所示:cookie
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace AspNetCoreCookieTest.Controllers { public class HomeController : Controller { private readonly ILogger<HomeController> _logger; public HomeController(ILogger<HomeController> logger) { _logger = logger; } /// <summary> /// Index視圖,用於對cookie進行添加、刪除、顯示 /// </summary> public IActionResult Index() { return View(); } /// <summary> /// 添加cookie /// </summary> public IActionResult AddCookie() { //添加兩個cookie:"Key01=&;鍵01"和"Key02=&;鍵02",這兩個cookie的key和value中都包含cookie特殊字符(諸如=&;等) Response.Cookies.Append("Key01=&;鍵01", "ABC=DEFG&H值01IJK;MLN"); Response.Cookies.Append("Key02=&;鍵02", "ABC\"EF值02G+123"); return View("Index"); } /// <summary> /// 刪除添加的cookie /// </summary> public IActionResult RemoveCookie() { Response.Cookies.Delete("Key01=&;鍵01"); Response.Cookies.Delete("Key02=&;鍵02"); return View("Index"); } /// <summary> /// 從ASP.NET Core代碼中獲取添加的cookie,並在視圖頁面中顯示獲取到的cookie值 /// </summary> public IActionResult ShowCookieInAspNetCore() { string Key01 = Request.Cookies["Key01=&;鍵01"]; string Key02 = Request.Cookies["Key02=&;鍵02"]; if (Key01 == null) { Key01 = string.Empty; } if (Key02 == null) { Key02 = string.Empty; } this.ViewData["Key01"] = string.Format("Cookie Key01=&;鍵01 value is {0}", Key01); this.ViewData["Key02"] = string.Format("Cookie Key02=&;鍵02 value is {0}", Key02); return View(); } } }
能夠看到,咱們在AddCookie方法中,添加了兩個Cookie鍵值對,而且咱們在Cookie的鍵和值中都加入了特殊字符,諸如"="、"&"、";"等。而後咱們稍後會用ShowCookieInAspNetCore方法在ASP.NET Core代碼中獲取添加的這兩個Cookie鍵值對,並在視圖頁面上進行顯示。測試
接下來咱們來看看Index.cshtml視圖頁面的代碼:this
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <script type="text/javascript" src="~/lib/jquery/dist/jquery.js" ></script> <script type="text/javascript"> $(function () { //添加cookie $("#btnAddCookie").click(function () { window.location = "@Url.Action("AddCookie")"; }); //在web瀏覽器中,採用JavaScript顯示添加的cookie $("#btnShowCookie").click(function () { alert(document.cookie); }); //刪除添加的cookie $("#btnRemoveCookie").click(function () { window.location = "@Url.Action("RemoveCookie")"; }); //從ASP.NET Core代碼中獲取添加的cookie,並在視圖頁面中顯示獲取到的cookie值 $("#btnShowAspNetCoreCookie").click(function () { window.location = "@Url.Action("ShowCookieInAspNetCore")"; }); }); </script> </head> <body> <div> <button id="btnAddCookie">添加Cookie</button> </div> <div style="margin-top:10px;"> <button id="btnShowCookie">用JavaScript顯示Cookie</button> </div> <div style="margin-top:10px;"> <button id="btnRemoveCookie">刪除Cookie</button> </div> <div style="margin-top:10px;"> <button id="btnShowAspNetCoreCookie">在ASP.NET Core中顯示Cookie</button> </div> </body> </html>
咱們在這個視圖中,定義了四個按鈕:編碼
如今咱們就來進行測試,執行項目,而後在web瀏覽器中訪問HomeController的Index方法,在顯示的視圖頁面中咱們先點擊添加Cookie按鈕:
這時ASP.NET Core執行了HomeController的AddCookie方法,因此咱們已經添加了兩個Cookie鍵值對。
接下來咱們在頁面上點擊用JavaScript顯示Cookie按鈕,這時咱們能夠看到儘管咱們添加的Cookie中鍵和值都包含特殊字符,可是實際上ASP.NET Core將Cookie鍵和值中的特殊字符都轉義了,進行了Url編碼,因此咱們在web瀏覽器中使用JavaScript顯示Cookie鍵值時,獲得的是進行Url編碼後的Cookie鍵值:
因此兩個Cookie鍵值對並無由於特殊字符而被錯誤地存儲到web瀏覽器中,鍵和值中全部的特殊字符都採用Url編碼進行了轉義。
接下來,咱們在頁面上點擊在ASP.NET Core中顯示Cookie按鈕,看看再從ASP.NET Core代碼中將這兩個Cookie鍵值對讀出來會是什麼結果:
這個按鈕會觸發HomeController的ShowCookieInAspNetCore方法,而後顯示ShowCookieInAspNetCore.cshtml視圖頁面,這個視圖頁面的代碼以下:
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>ShowCookieInAspNetCore</title> </head> <body> <div> @ViewData["Key01"].ToString() </div> <div style="margin-top:10px;"> @ViewData["Key02"].ToString() </div> <div style="margin-top:10px;"> <a href="@Url.Action("Index")">返回Index視圖頁面</a> </div> </body> </html>
這個視圖頁面其實就是將ASP.NET Core代碼獲取到的兩個Cookie的值顯示出來。
該視圖頁面執行結果以下:
同時咱們在Visual Studio中添加斷點,在調試模式下,監測到了HomeController的ShowCookieInAspNetCore方法中獲取到的兩個Cookie值:
能夠看到無論是ShowCookieInAspNetCore.cshtml視圖頁面,仍是Visual Studio的調試模式,都顯示ASP.NET Core代碼獲取到的兩個Cookie鍵值對,都自動進行了Url解碼,和前面咱們添加Cookie時的鍵和值徹底相同。
因此這說明當咱們在ASP.NET Core中存儲Cookie鍵值到web瀏覽器時,ASP.NET Core代碼會自動將Cookie鍵值中的特殊字符采用Url編碼進行轉義,而稍後咱們用ASP.NET Core代碼讀取Http請求中的Cookie鍵值時,ASP.NET Core代碼又會自動將Cookie鍵值中的特殊字符采用Url解碼進行還原。
因此在ASP.NET Core中將特殊字符存入Cookie鍵值對是沒有問題的,只不過若是在客戶端web瀏覽器中使用JavaScript獲取Cookie時,獲取到的將會是Url編碼後的Cookie鍵和值。可是從ASP.NET Core代碼的角度去讀寫Cookie鍵和值是徹底沒有問題的,由於ASP.NET Core代碼會對Cookie鍵和值中的特殊字符自動進行Url編碼和Url解碼。