【问题标题】:Error with HttpCookie in Asp.net MVCAsp.net MVC 中的 HttpCookie 错误
【发布时间】: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


【解决方案1】:

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

修复 - 指定完整的类名:

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

【讨论】:

  • 我试过这条线。它给了我错误:无法将类型 System.Web.HttpCookie' 隐式转换为'RestSharp.HttpCookie'。我的 API 调用必须使用“使用 RestSharp”。这让我很困惑哈哈。
  • 将我的用途添加到原始帖子中
  • 好的,我找到了答案,谢谢你的帮助。让我走上正确的轨道@Alexei Levenkov
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 2011-11-22
  • 2016-08-20
  • 2021-07-29
  • 2016-06-26
相关资源
最近更新 更多