Memungkinkan Anda menambahkan cookie untuk ihttpactionResult di pengontrol webapi.
Isi
Ada beberapa cara untuk menambahkan cookie ke respons di WebAPI. Cara yang disarankan, menurut dokumen, adalah dengan menggunakan metode ekstensi resp.Headers.AddCookies(cookies) , tetapi ada beberapa kelemahan:
= char dalam nilai cookie.CookieHeaderValue mendukung pasangan nilai-nama dan koleksi tersebut akan disajikan sebagai cookie-name=key1=value1&key2=value2 , tetapi koleksi akan dikodekan jika Anda mencoba untuk mengaturnya hanya dengan hanya lewat string. Melewati string pengumpulan cookie secara langsung berguna untuk kasus ketika Anda melewati nilai cookie melalui layanan, misalnya integrasi dengan API berbasis cookie lawas. Cara lain adalah mengatur cookie di httpresponse.cookies melalui HttpContext (contoh periksa), tetapi ada kerugian yang lebih serius:
HttpContext di WebAPI adalah praktik yang buruk, karena Anda tidak dapat mendapatkannya di host sendiri.new Thread() . Lebih baik memiliki API sederhana untuk IHttpActionResult tanpa kerugian yang dijelaskan. Juga bagus untuk memiliki dukungan localhost atau "mengaktifkan cookie ini untuk semua subdomain" fitur di luar kotak.
Anda dapat menginstal paket aspnet.webapi.cookiespassthrough melalui Nuget.
var cookieDescriptors = new [ ]
{
// simple cookie with Path=/
new CookieDescriptor ( "test-cookie" , "1" ) ,
// encode
new CookieDescriptor ( "test-cookie2" , "2=" ) {
CodeStatus = CookieCodeStatus . Encode
} ,
// expires, secure, httponly + decode
new CookieDescriptor ( "test-cookie3" , "a%3D3" ) {
Secure = true ,
CodeStatus = CookieCodeStatus . Decode ,
HttpOnly = true ,
Expires = new DateTime ( 2118 , 1 , 1 )
} ,
// path will be added and no decode or encode
new CookieDescriptor ( "test-cookie4" , "4%3D=" ) {
Path = "/subfolder/"
} ,
} ;
// also you can use Request.GetReferrerHost() to get referrer's host which is useful when you're developing AJAX API
return Ok ( ) . AddCookies ( cookieDescriptors , Request . GetRequestHost ( ) ) ;Anda dapat mengaktifkan cookie untuk semua subdomain:
// domain will be ".example.org"
return Ok ( ) . AddCookies ( cookieDescriptors , "example.org" ) . EnableCookiesForAllSubdomains ( ) ;
// same, domain will be ".example.org"
return Ok ( ) . AddCookiesForAllSubdomains ( cookieDescriptors , "www.example.org" ) ;
// or even this
return Ok ( )
. AddCookiesForAllSubdomains ( cookieDescriptorsForAllSubdomains , "example.org" )
. AddCookies ( cookieDescriptorsForOneDomain , "example.com" )
. AddCookies ( cookieDescriptorsForAnotherDomainAndAllSubdomains , "www.example.net" )
. EnableCookiesForAllSubdomains ( ) ; Browser memiliki masalah dengan cookie localhost. Jika Anda akan menentukan domain sebagai localhost atau bahkan .localhost itu tidak akan ditambahkan ke respons sama sekali untuk membuat cookie dengan pekerjaan localhost untuk hampir semua browser.
Saat Anda menelepon .EnableCookiesForAllSubdomains() atau gunakan .AddCookiesForAllSubdomains(...) Konvertion domain berikut akan diterapkan:
"localhost" => " "
" . localhost " => " "
"www.localhost" => ".www.localhost"
"www.localhost.ru" => ".localhost.ru"
"www.org" => ".www.org"
".www.org" => ".www.org"
"example.org" => ".example.org"
"www.example.org" => ".example.org"
".www.example.org" => ".www.example.org" Periksa AspNet.WebApi.CookiesPassthrough.Example Project.