【发布时间】:2014-04-01 04:08:18
【问题描述】:
我已经研究了很长时间,但无法弄清楚为什么我的 HttpCookie 会出现这两个错误。
我的第一个错误是在这一行:
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName,
encryptedTicket
);
错误是
RestSharp.HttpCookie不包含带 2 个参数的构造函数。
第二行如下:
Response.Cookies.Add(cookie);
错误是
最好的重载方法匹配 System.Web.HttpCookieCollection.Add(System.Web.HttpCookie) 有一些 无效参数
这是我的登录方式:
[HttpPost]
public ActionResult Login(Models.User user)
{
bool isPersistent = false;
var client = new RestClient("localhost");
var request = new RestRequest("api/myapi/", Method.GET);
request.AddHeader("email-header", user.Email);
request.AddHeader("password-header", user.Password);
var response = client.Execute(request) as RestResponse;
var content = response.Content;
var result = content.Split('"');
var userId = result[7];
var apiKey = result[3];
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Sets ticket number
userId, // Authenticated user Id
DateTime.Now, // Issue date
DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes), // Expiration date
isPersistent, // Persists across browser sessions
apiKey, // Authenticated Api Key
FormsAuthentication.FormsCookiePath); // Path for the cookie
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
// Fails here
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.HttpOnly = true;
//Fails here
Response.Cookies.Add(cookie);
Response.Redirect(FormsAuthentication.GetRedirectUrl(user.Email, isPersistent));
return View();
}
我正在使用什么:
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
//using System.Web;
using System.Web.HttpResponse;
using System.Web.Mvc;
using System.Web.Security;
解决我的问题:
不得不更换
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);
有
System.Web.HttpCookie cookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);
【问题讨论】:
-
还有什么错误!
-
哈哈抱歉,第一个是 'RestSharp.HttpCookie' 不包含包含 2 个参数的构造函数。
-
旁注:在帖子中添加错误文本时,请尝试重新格式化您的示例以避免水平滚动。
-
第二个是,'System.Web.HttpCookieCollection.Add(System.Web.HttpCookie)'的最佳重载方法匹配有一些无效参数
-
@AlexeiLevenkov Roger 那会记住下一篇文章。
标签: c# asp.net asp.net-mvc forms-authentication httpcookie