使用ASP.NET Web API读取Cookie [英] Reading cookie using asp.net web api

查看:86
本文介绍了使用ASP.NET Web API读取Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用

document.cookie = "username=John Doe; expires=Thu, 18 Dec 2019 12:00:00 UTC";

我想使用ASP.NET Web API(在.NET 4.5上运行)在服务器端读取此cookie,但是当我检查请求对象时,却看不到该cookie.

I want to read this cookie on server side using ASP.NET Web API (running on .NET 4.5) but when I inspect request object, I do not see the cookie.

推荐答案

我不确定您指的是Asp.Net的哪个版本,但根据:

I'm not sure which version of Asp.Net you are referring to but according to:

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/http-cookies

您可以使用以下方式检索Cookie:

You can retrieve cookies using something like:

var userName = Request.Headers.GetCookies("userName").FirstOrDefault()?["userName"].Value;

在Web api 2控制器中,您还需要对 System.Net.Http 的引用.

In a web api 2 controller you will also need a reference to System.Net.Http.

这篇关于使用ASP.NET Web API读取Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆