0

i have been researching for a good while now and cannot figure out why I am getting these 2 errors with my HttpCookie.

My first error is in this line :

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

and error is

RestSharp.HttpCookie does not contain a constructor that takes 2 arguments.

And the 2nd line is below:

Response.Cookies.Add(cookie);

and error is

The best overloaded method match for System.Web.HttpCookieCollection.Add(System.Web.HttpCookie) has some invalid arguments

This is my login method:

[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();

}

What I am using:

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;

Solution to my problem:

Had to replace

 HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
        cookie.HttpOnly = true;
        Response.Cookies.Add(cookie);

With

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

            cookie.HttpOnly = true;
            Response.Cookies.Add(cookie);
ssilas777
  • 9,672
  • 4
  • 45
  • 68
Shawn
  • 2,355
  • 14
  • 48
  • 98

1 Answers1

2

You are using different class than you expect - it looks like you have code that should be using System.Web.HttpCookie, but instead it picked up RestSharp.HttpCookie.

Fix - specify full class name:

  var cookie = new System.Web.HttpCookie(
      FormsAuthentication.FormsCookieName, encryptedTicket);
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • I tried this same line. It gives me the error: Cannot implicitly convert type System.Web.HttpCookie' to 'RestSharp.HttpCookie'. I have to use "using RestSharp" for my API call tho. This is what is confusing me lol. – Shawn Apr 01 '14 at 04:24
  • Added my uses to original post – Shawn Apr 01 '14 at 04:25
  • Ok I found the answer, thank you for the help. Got me on the right track @Alexei Levenkov – Shawn Apr 01 '14 at 04:31