1

I am using cookie for the authentication in web api. Creating cookies in the server side web api. and when i am reading it shows me null. also, i am sending a request using html ajax.

Function using to create cookie:

public HttpResponseMessage SetCookies()
    {
        var resp = new HttpResponseMessage()`enter code here`;

        HttpClient client = new HttpClient(handler);

        var cookie = new CookieHeaderValue("MyCookie", "12345");
        cookie.Expires = DateTimeOffset.Now.AddDays(1);
        cookie.Domain = Request.RequestUri.Host;
        cookie.Path = "/";

        resp.Headers.AddCookies(new CookieHeaderValue[] { cookie });
        return resp;
    } 

after creating the cookies i try to reading cookie using below code:

 CookieHeaderValue cookie = Request.Headers.GetCookies("MyCookie").FirstOrDefault();
            if (cookie != null)
            {
                string sessionId = cookie["MyCookie"].Value;
            }

it always returns null.

Please help me how to get and set the cookie value.

Thank you.

Here is the java script code :

function setCookie() {
    var cname = " MyCookie";
    var cvalue = "1234fer5678";
    var exdays = 1;
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
function onButtonClick() {
    setCookie();`enter code here`
    $.ajax({
        url: 'http://localhost:49702/v1/user/register',
        type: "GET",
        data: {},
        dataType: 'json',
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true
    });      

};

I set the cookies value manually. still i get the cookies in header request null on the server side api

user3369120
  • 89
  • 1
  • 13

1 Answers1

0

I had seen your javascript code, and in the code crossDomain: true, I think your problem is about the cross domain, you can try this :

$.ajax({
        url: 'http://localhost:49702/v1/user/register',
        type: "GET",
        data: {},
        dataType: 'jsonp', // Notice! jsonp (lowercase)
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true
    });    

update: look the code blow , you can set the domain when you set cookie.

var cookieName = 'HelloWorld';
var cookieValue = 'HelloWorld';
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 12);
document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate 
                  + ";domain=.example.com;path=/";
gangzi
  • 105
  • 1
  • 13
  • I tried the above code and tried with cross domain true and false both but it doesnt working gives same null record. – user3369120 Aug 19 '15 at 04:25
  • you can read this link,if then [basic-example-of-using-ajax-with-jsonp](http://stackoverflow.com/questions/5943630/basic-example-of-using-ajax-with-jsonp) – gangzi Aug 19 '15 at 05:05
  • set the cookie as you said but no luck. still shows cookies null. – user3369120 Aug 19 '15 at 16:39
  • When i try to get the cookie in simple controller like the below code i am able to get the cookies but when i try to get the cookies in api controllers i get the cookies null – user3369120 Aug 19 '15 at 16:48
  • protected override void OnActionExecuting(ActionExecutingContext filterContext) { if (HttpContext.Request.Cookies.AllKeys.Contains("MyCookie")) { Session["MyCookie"] = HttpContext.Request.Cookies["MyCookie"].Value; } base.OnActionExecuting(filterContext); } – user3369120 Aug 19 '15 at 16:51