【C# HttpClient设置cookies的两种办法】教程文章相关的互联网学习教程文章

C# HttpClient设置cookies的两种办法【代码】

前言:最近公司使用HttpClient对象在发送请求,抛弃了之前的HttpWebRequest,使用httpClient有个好处:就是可以只使用一个HttpClient的实例,去完成发送所有的请求数据(地址,请求数据,类型,Cookie等)。 而传统的HttpWebRequest每次请求需要创建一个单独的实例,且规避了一个老问题:Socket 连接释放不及时的问题。 下面的是转发其他道友的文章,主要是在使用的过程中,自身需要发送一个自定义的Cookie到目标服务器,但是...

如果cookie存在,即使它是在另一个应用程序中创建的,如何检查? (使用JS或C#)【代码】

我有几个应用程序,其中一个是管理身份验证的中央应用程序,其中LogOn页面作为IFrame导入到其他应用程序. 当userName和密码正确时,我创建一个名为userInfo的cookie. 现在,在当前的应用程序中,我想检查userInfo的cookie是否存在.我想我应该检查它是否存在于浏览器中(在客户端). 它必须是可能的,所以我该怎么做呢? 提前致谢.解决方法:Cookie不能跨域共享.如果您的应用程序不在同一个域中托管,则必须忘记这一点.它不起作用,因为浏览器(...

c# – AspNet核心身份,如何设置options.Cookie.SameSite?【代码】

在最新的模板和库中使用了httpsonly标志.我怎么能把它关掉? 同样的问题已经过时,它没有完整的配置示例: AspNet Core Identity – cookie not getting set in production解决方法:为了在使用Identity时配置应用程序cookie,您可以在Startup的ConfigureServices中使用ConfigureApplicationCookie method:// add identity services.AddIdentity<ApplicationUser, IdentityRole>();// configure the application cookie services.Con...

c# – 如何在静态web方法中编写cookie

我正在使用John Culviner的jquery.filedownload插件. 为了工作,我需要写一个cookie.如何在静态web方法中编写cookie?作为Page. Response是一个实例字段,我无法从静态方法访问它.解决方法:在WebMethod中使用HttpContext.Current属性.

c# – 如何在OWIN CookieAuthenticationProvider中更新声明?【代码】

在使用OWIN管道的ASP.NET应用程序中,我尝试使用cookie身份验证&覆盖CookieAuthenticationProvider.ValidateIdentity并执行类似的操作:public override Task ValidateIdentity(CookieValidateIdentityContext context) {Claim simpleClaim = context.Identity.FindFirst("SIMPLECOUNT");if (simpleClaim != null) {Trace.WriteLine($"SIMPLECOUNT: {simpleClaim.Value}");if (context.Identity.TryRemoveClaim(simpleClaim)) {var ...

c# – WP 7.1上的Restsharp添加cookie【代码】

我正在尝试使用RestSharp向请求添加cookie但在fiddler2中我没有看到请求和服务调用中的cookie失败.向RestRequest添加cookie有一些技巧吗?RestRequest rq = new RestRequest(LTV.NowNext(), Method.GET);rc.AddDefaultParameter(LTV.cookie.Key, LTV.cookie.Value, ParameterType.Cookie);rc.ExecuteAsync<LTV.nowNext>(rq, (response2) =>{if (response2.Data == null)return;foreach (LTV.channel channel in response2.Data.cha...

c# – 设置和获取cookie asp.net mvc4【代码】

我正在尝试在我正在使用asp.net mvc4的项目中设置并获取cookie. 这就是我设置cookie的方式:var Username = new HttpCookie("Username");Username.Value = model.UserName;Username.Expires = DateTime.Now.AddDays(7);Username.Secure = true;Response.Cookies.Add(Username);然后,在其他控制器操作中,我正在尝试这个:HttpCookie cookie = Request.Cookies["Username"];但是我为那个特定的cookie得到了null.此外,我不知道这是否会...

c# – 在WebAPI和asp.net核心中使用基于Cookie的身份验证【代码】

场景: 我有一个解决方案,其中,我有WebAPI和Asp.Net核心MVC项目.我在WebAPI中实现了基于Cookie的身份验证.使用Postman进行测试时效果很好.但是,当我从我的MVC项目中使用WebAPI服务时,身份验证似乎被打破了. 这是我的代码: 的WebAPI: Startup.csapp.UseCookieAuthentication(new CookieAuthenticationOptions() {AuthenticationScheme = "ApiAuth",AutomaticAuthenticate = true,AutomaticChallenge = false });AccountController...

c# – 如何根据cookie值将用户对象从启动类注入到控制器构造函数中【代码】

我正在使用CookieAuthentication来实现自定义登录,app.UseCookieAuthentication(new CookieAuthenticationOptions() {AuthenticationScheme = "MyCookieMiddlewareInstance",LoginPath = new PathString("/Account/Unauthorized/"),AccessDeniedPath = new PathString("/Account/Forbidden/"),AutomaticAuthenticate = true,AutomaticChallenge = true,CookieName="Id", });我的应用程序中的每个控制器都期望一个用户对象,可以使用...

c# – 更新的Cookie无法在Response.Redirect上运行【代码】

我正在更新cookie如下if (Request.Cookies["SSOPortalUser"] == null){HttpCookie myCookieSSOPortalUser = new HttpCookie("SSOPortalUser");// Set the cookie value.myCookieSSOPortalUser.Value = currentUser.UserLogin.ToString();// Add the cookie.Response.Cookies.Add(myCookieSSOPortalUser);}else{Request.Cookies["SSOPortalUser"].Value = currentUser.UserLogin.ToString();}但是在我重定向到另一个页面后,浏览器中...

c# – ASP.NET身份验证cookie【代码】

在具有基于表单的身份验证的应用程序上,我有一个标准的ASP.NET登录控件,其中包含以下Authenticate事件处理程序.void Login_Authenticate(object sender, AuthenticateEventArgs e) {if (Security.AuthenticateUser(Login.UserName, Login.Password)){e.Authenticated = true;RedirectFromLoginPage(Login.UserName);}else{e.Authenticated = false;} }RedirectFromLoginPage函数如下所示:private void RedirectFromLoginPage(Stri...

c# – 为什么我不能使用HttpContext或HttpCookie? (Asp.Net Core 1.0)【代码】

为什么我不能使用HttpContext或HttpCookie?有特殊用途吗? 我的实际使用情况:using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;我的名称:namespace eCoffee.Main我的班级方法:public class AppRunCookie {public static string CookieName { get; set; }public static string Key { get; set; } public AppRunCookie(string key) {CookieName = "MyCookie_" + key; }public ...

c# – .NET Compact Framework – 基于Cookie的Web服务访问

我需要从.NET Compact Framework 3.5应用程序访问Web服务.问题是Web Service使用cookie进行身份验证.在桌面应用程序中,我使用了.NET中缺少的.NET CookieContainer().如何在没有CookieContainer的情况下管理CF中的cookie? 有人可以帮我解决这个问题吗?谢谢.马修解决方法:您需要直接使用cookie HTTP标头. 这篇论坛帖子有一个合理的代码示例,可以帮助您入门: Cookies in Compact Framework 有关Cookie HTTP标头的更多常规信息,请查...

c# – 使用cookie和viewState将数据发布到网站【代码】

我想将数据发布到网站上.我使用以下代码处理cookie:CookieCollection cookies = new CookieCollection();request.CookieContainer = new CookieContainer();request.CookieContainer.Add(cookies); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);我使用以下代码处理viewstate值:var doc = new HtmlAgilityPack.HtmlDocument();doc.Load(resp.GetResponseStream());foreach (HtmlNode input in doc.Docum...

c# – 为什么httpRuntime targetFramework =“4.5”禁用抓取.ASPXAUTH cookie?【代码】

当我的web.config具有以下httpRuntime时,我的控制器无法获取cookie .ASPXAUTH.它似乎能够抓取任何其他cookie,有或没有句点前缀.如果我删除下面的行,它工作正常.<httpRuntime targetFramework="4.5"/>我正在使用以下内容来获取cookie.HttpCookie authCookie = Request.Cookies[".ASPXAUTH"];为什么我不能获取Forms Authentication cookie?解决方法:我有类似的问题 – 我的应用程序与运行时4.5无法读取由4.0下运行的另一个/ login /...

HTTPCLIENT - 相关标签