Javascript Cookie在web api中返回null
我在web api中使用cookie进行身份验证。在服务器端web api中创建cookie。当我阅读时,它显示我为空。 另外,我正在使用html ajax发送请求 用于创建cookie的函数:Javascript Cookie在web api中返回null,javascript,cookies,asp.net-web-api,Javascript,Cookies,Asp.net Web Api,我在web api中使用cookie进行身份验证。在服务器端web api中创建cookie。当我阅读时,它显示我为空。 另外,我正在使用html ajax发送请求 用于创建cookie的函数: public HttpResponseMessage SetCookies() { var resp = new HttpResponseMessage()`enter code here`; HttpClient client = new HttpClient
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;
}
创建cookie后,我尝试使用以下代码读取cookie:
CookieHeaderValue cookie = Request.Headers.GetCookies("MyCookie").FirstOrDefault();
if (cookie != null)
{
string sessionId = cookie["MyCookie"].Value;
}
它总是返回null
请帮助我如何获取和设置cookie值
多谢各位
以下是java脚本代码:
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
});
};
我手动设置cookies值。但我仍然在服务器端api上得到了标头请求null中的cookies,我看到了您的javascript代码,在代码
跨域:true
中,我认为您的问题是关于跨域的问题,您可以尝试以下方法:
$.ajax({
url: 'http://localhost:49702/v1/user/register',
type: "GET",
data: {},
dataType: 'jsonp', // Notice! jsonp (lowercase)
xhrFields: {
withCredentials: true
},
crossDomain: true
});
更新:查看代码,您可以在设置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=/";
我看过你的javascript代码,在代码crossDomain:true
,我想你的问题是关于跨域的问题,你可以试试这个:
$.ajax({
url: 'http://localhost:49702/v1/user/register',
type: "GET",
data: {},
dataType: 'jsonp', // Notice! jsonp (lowercase)
xhrFields: {
withCredentials: true
},
crossDomain: true
});
更新:查看代码,您可以在设置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=/";
你能把你的javascript代码放在这里吗?我认为js的一些问题是可以的。添加的java脚本代码@Gangzi你能把你的javascript代码放在这里吗?我认为js的一些问题是可以的。添加的java脚本代码@gangziI尝试了上面的代码,并尝试了跨域true和false,但都不起作用,给出了相同的空记录。你可以阅读此链接,如果没有,则按照您所说的设置cookie,但没有运气。仍然显示cookies null。当我尝试在简单控制器(如下面的代码)中获取cookie时,我能够获取cookie,但当我尝试在api控制器中获取cookie时,我获取cookies null受保护的覆盖void OnActionExecuting(ActionExecutingContext filterContext){if(HttpContext.Request.cookies.AllKeys.Contains(“mycokie”){Session[“mycokie”]=HttpContext.Request.Cookies[“mycokie”].Value;}base.OnActionExecuting(filterContext);}我尝试了上面的代码,并尝试了跨域true和false,但它不起作用,给出了相同的空记录。你可以阅读此链接,如果然后按照你说的设置cookie,但没有运气。仍然显示cookies null。当我尝试在简单控制器(如下面的代码)中获取cookies时,我能够获取cookies,但当我尝试在pi控制器我得到cookies nullprotected override void OnActionExecuting(ActionExecutingContext filterContext){if(HttpContext.Request.cookies.AllKeys.Contains(“mycokie”){Session[“mycokie”]=HttpContext.Request.cookies[“mycokie”].Value;}base.OnActionExecuting(filterContext);}