Carl Forgey
Carl Forgey

Reputation: 178

ASP.NET Cookies are being set with no value

I am trying to pass data from the login controller to the home controller via a cookie. This seems like it shouldn't be a big deal, but the cookie is being written with no value. Quite frustrating.

The code I'm using to set the cookie:

Customer user = new Customer();
int pass;
pass = MD5Hash(formResult.password);
if (pass == customer.CM_PASSWORD_HASH)
{
    FormsAuthentication.SetAuthCookie(customer.CM_FULL_NAME, true);
    HttpCookie userCookie = new HttpCookie("User");

    userCookie.Values.Add("id", customer.CM_CUSTOMER_ID.ToString());
    userCookie.Values.Add("companyid", customer.CM_COMPANY_ID.ToString());
    userCookie.Values["type"] = "customer";
    userCookie.Values["name"] = customer.CM_FULL_NAME;

    HttpContext.Response.Cookies.Set(userCookie);

    return RedirectToAction("Index", "Home");
}

This is what I'm trying to use to read the cookie:

if (Request.Cookies.AllKeys.Contains("User"))
{
    HttpCookie userCookie = HttpContext.Request.Cookies["User"];
    switch (userCookie.Values["type"])
    {
        case "customer":
            Customer customer = new Customer();
            customer.BridgeTrakId = Int32.Parse(Response.Cookies.Get("User").Values["id"]);
            customer.CompanyId = Int32.Parse(Response.Cookies.Get("User").Values["companyid"]);
            customer.Name = Response.Cookies.Get("User").Values["name"];
            model.User = customer;
            model.Equipment = DataAccess.EquipmentRepository.GetEquipment(customer.BridgeTrakId);
        break;
        default:
            break;
    }
}

The cookie gets created, and when I view it in the Chrome tools, the name is there, but the value is empty. When I view the userCookie in visual studio, it shows that it has a value.

Can anyone explain to me where I am losing the value, and how I can fix this.

Upvotes: 0

Views: 799

Answers (2)

Win
Win

Reputation: 62300

You want to retrieve id, companyid and name same as you did for type.

HttpCookie userCookie = HttpContext.Request.Cookies["User"];
switch (userCookie.Values["type"])
{
    case "customer":
        Customer customer = new Customer();
        customer.BridgeTrakId = Int32.Parse(userCookie.Values["id"]);
        customer.CompanyId = Int32.Parse(userCookie.Values["companyid"]);
        customer.Name = userCookie.Values["name"];
        ...
        break;
    default:
        break;
}

Another Method

Request.Cookies["User"]["type"]
Request.Cookies["User"]["id"]
Request.Cookies["User"]["companyid"]
Request.Cookies["User"]["name"]

Note: I do not know the reason behind passing data. Ideally, you do not want to cookie just to pass data between controller. Instead, you want to use QueryString or SessionState.

Upvotes: 1

KevDevMan
KevDevMan

Reputation: 818

Try setting the cookie like so.

        HttpCookie cookie = new HttpCookie("Cookie");
        cookie.Value = "Hello Cookie! CreatedOn: " + DateTime.Now.ToShortTimeString();

        this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
        return RedirectToAction("Index", "Home");

Upvotes: 1

Related Questions