2014-04-01 54 views
0

我一直在研究很長一段時間,不知道爲什麼我得到這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); 
+2

,什麼是錯誤! – ssilas777

+0

對不起,第一個是'RestSharp.HttpCookie'不包含包含2個參數的構造函數。 – Shawn

+1

備註:在向文章添加錯誤文本的同時,嘗試重新格式化您的示例以避免橫向滾動。 –

回答

1

您使用的分類比您預期的要多 - 看起來您的代碼應該使用System.Web.HttpCookie,但它代碼爲RestSharp.HttpCookie

修復 - 指定完整的類名:

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

我試過這一行。它給了我錯誤:不能隱式地將類型System.Web.HttpCookie轉換爲'RestSharp.HttpCookie'。我必須使用「使用RestSharp」來進行我的API調用。這是讓我困惑的東西。 – Shawn

+0

將我的用途加入原文 – Shawn

+0

好的,我找到了答案,謝謝你的幫助。讓我在正確的軌道@Alexei Levenkov – Shawn