Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.net MVC中的HttpCookie错误_C#_Asp.net_Asp.net Mvc_Forms Authentication_Httpcookie - Fatal编程技术网

C# Asp.net MVC中的HttpCookie错误

C# Asp.net MVC中的HttpCookie错误,c#,asp.net,asp.net-mvc,forms-authentication,httpcookie,C#,Asp.net,Asp.net Mvc,Forms Authentication,Httpcookie,我已经研究了很长一段时间了,但我不明白为什么我的HttpCookie会出现这两个错误 我的第一个错误在这一行: HttpCookie cookie = new HttpCookie( FormsAuthentication.FormsCookieName, encryptedTicket

我已经研究了很长一段时间了,但我不明白为什么我的HttpCookie会出现这两个错误

我的第一个错误在这一行:

    HttpCookie cookie = new HttpCookie(
                                        FormsAuthentication.FormsCookieName,
                                        encryptedTicket
                                      );
错误是

RestSharp.HttpCookie
不包含接受2个参数的构造函数

第二行如下:

Response.Cookies.Add(cookie);
错误是

匹配的最佳重载方法 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);


您使用的类与预期的不同-看起来您有应该使用的代码,但它选择了
RestSharp.HttpCookie

修复-指定完整类名:

  var cookie = new System.Web.HttpCookie(
      FormsAuthentication.FormsCookieName, encryptedTicket);

什么是错误!哈哈,很抱歉,第一个是'RestSharp.HttpCookie'不包含包含2个参数的构造函数。旁注:在向帖子添加错误文本时,请尝试重新格式化示例以避免水平滚动。第二个是,与'System.Web.HttpCookieCollection.Add(System.Web.HttpCookie)最匹配的重载方法“有一些是无效的arguments@AlexeiLevenkov收到,我会记得下一篇文章的。我也试过这句话。它给了我一个错误:无法将System.Web.HttpCookie类型隐式转换为RestSharp.HttpCookie类型。我必须在API调用中使用“using RestSharp”。这就是让我困惑的地方。把我的用法添加到原来的postOk中我找到了答案,谢谢你的帮助。让我走上正轨@Alexei Levenkov
  var cookie = new System.Web.HttpCookie(
      FormsAuthentication.FormsCookieName, encryptedTicket);