WCF 4.0 Cookie Only First is Recorded by Browser

后端 未结 2 1000
[愿得一人]
[愿得一人] 2021-01-19 10:02

I am working on the fastest way to write cookies from a WCF self hosted console app REST service with WebHttpBinding. I prepare the \"Set Cookies\" but only the first cookie

2条回答
  •  [愿得一人]
    2021-01-19 10:58

    To expand on @jeff-fischer 's answer, AspNetCompatibilityMode does work and requires the following:

    AspNetCompatibilityRequirements is set for the service class to either Allowed or Required e.g.:

    [AspNetCompatibilityRequirements(RequirementsMode
           = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AppService : IAppService
    

    is set in

    This then gives access to HttpContext (you'll need using System.Web; to get access to this) and cookies can be set using:

    var aCookie = new HttpCookie("foo")
    {
        HttpOnly = true,
        Value = "bar",
        Expires = DateTime.Now.AddDays(1)
    };
    
    HttpContext.Current.Response.Cookies.Add(aCookie);
    

    This will then need to be run as an application on IIS rather than though the WCF launcher and if multiple cookies are set, multiple cookie headers will actually appear.

提交回复
热议问题