我一直在研究很長一段時間,不知道爲什麼我得到這2個錯誤與我的HttpCookie。在Asp.net MVC中的HttpCookie錯誤
我的第一個錯誤是在這一行:
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName,
encryptedTicket
);
和錯誤是
RestSharp.HttpCookie
does not contain a constructor that takes 2 arguments.
而二號線低於:
Response.Cookies.Add(cookie);
和錯誤是
The best overloaded method match for System.Web.HttpCookieCollection.Add(System.Web.HttpCookie) has some invalid arguments
這是我的登錄方法:
[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);
,什麼是錯誤! – ssilas777
對不起,第一個是'RestSharp.HttpCookie'不包含包含2個參數的構造函數。 – Shawn
備註:在向文章添加錯誤文本的同時,嘗試重新格式化您的示例以避免橫向滾動。 –