1
我試圖在驗證身份驗證後向用戶發送cookie。一切正常,響應正在我的代碼中構建,但即使在客戶端得到響應後,瀏覽器中也沒有保存cookie(通過chrome F12 - > Resources查看)。在發佈回覆中發送Cookie WebAPI
注:我可以看到應答提琴手被送到我的餅乾:
不知什麼錯誤,爲什麼瀏覽器不保存的Cookie。
這裏是的WebAPI函數處理POST請求:
public HttpResponseMessage Post([FromBody]User user)
{
IDal dal = new ProGamersDal();
var currentUser = dal.GetUser(user.Username, user.Password);
if (currentUser == null)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Bad request.");
}
else
{
var res = new HttpResponseMessage();
var cookie = new CookieHeaderValue("user",JsonConvert.SerializeObject(new ReponseUser(){username = currentUser.Username, role = currentUser.Role}));
cookie.Expires = DateTimeOffset.Now.AddDays(1);
cookie.Domain = Request.RequestUri.Host;
cookie.Path = "/";
res.Headers.AddCookies(new CookieHeaderValue[] { cookie });
return res;
}
}
您的圖片顯示「請求「標題......」響應「標題中有什麼值? –
請求頭也包含cookie –