يتيح لك إضافة ملفات تعريف الارتباط لـ IHTTPactionResult في وحدات التحكم في WebAPI.
محتويات
هناك عدة طرق لإضافة ملفات تعريف الارتباط إلى الاستجابة في WebAPI. الطريقة الموصى بها ، وفقًا لمستندات المستندات ، هي استخدام طريقة تمديد resp.Headers.AddCookies(cookies) ، ولكن هناك بعض العيوب:
= char في قيمة ملفات تعريف الارتباط.CookieHeaderValue أزواج القيمة الأسماء وسيتم تقديم هذه المجموعات كملف cookie-name=key1=value1&key2=value2 ، ولكن سيتم ترميز المجموعة إذا حاولت تعيينها عبر سلسلة تمرير فقط. يعد تمرير سلاسل مجموعة ملفات تعريف الارتباط مباشرة مفيدة للحالات التي تمرر فيها قيم ملفات تعريف الارتباط من خلال الخدمات ، مثل التكامل مع واجهات برمجة التطبيقات القائمة على ملفات تعريف الارتباط. هناك طريقة أخرى تتمثل في تعيين ملفات تعريف الارتباط على httpresponse.cookies عبر HttpContext (تحقق مثال) ، ولكن هناك عيوب أكثر خطورة:
HttpContext في WebAPI ممارسة سيئة ، لأنه لا يمكنك الحصول عليها في المضيف الذاتي.new Thread() . من الأفضل أن يكون لديك واجهة برمجة تطبيقات بسيطة لـ IHttpActionResult مع العيوب الموصوفة. من الجيد أيضًا الحصول على دعم مضيف محلي أو "تمكين ملفات تعريف الارتباط هذه لجميع المجالات الفرعية".
يمكنك تثبيت ASPNET.WEBAPI.COOKIESSPASSTHOUGH عبر 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 ( ) ) ;يمكنك تمكين ملفات تعريف الارتباط لجميع النطاقات الفرعية:
// 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 ( ) ; المتصفحات لديها مشاكل مع ملفات تعريف الارتباط المحلية. إذا كنت ستحدد المجال على أنه localhost أو حتى .localhost ، فلن تتم إضافته إلى الاستجابة على الإطلاق لجعل ملفات تعريف الارتباط مع عمل مضيف محلي لجميع المتصفحات تقريبًا.
عندما تتصل .EnableCookiesForAllSubdomains() أو استخدام .AddCookiesForAllSubdomains(...) سيتم تطبيق تحويل المجال التالي:
"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" تحقق من AspNet.WebApi.CookiesPassthrough.Example .