I only seem to be able to set one Cookie - HttpCookie, asp.net

105 views Asked by At

I have a system with a two-stage login.

Stage one is a Company Login which identifies the Company using my system. Stage two is a Staff Login where a member of staff belonging to the above company logs in.

In both stage an option is offered to save certain login details (Location/Company but not password)

A user should be able, if they wish, to set the Company Login Cookie, but not the Staff Cookie, there is no need to set a Staff Cookie without a Company Cookie, although it doesn't really matter if they do!

Stage one login, amongst database checks etc does this:

 If SaveCookie Then
    Dim loginCookie As New HttpCookie("LogInCompany")
    loginCookie.Values("database") = Database
    loginCookie.Values("savedKey") = SavedKey
    loginCookie.Values("samCompanyId") = CompanyId
    loginCookie.Values("samCompanyName") = Common.htmlDecode(CompanyName)
    loginCookie.Expires = Date.Now.AddDays(7)
    HttpContext.Current.Response.Cookies.Add(loginCookie)
End If

Then stage two does this:

If SaveCookie Then
    Dim loginCookie As New HttpCookie("LogInStaff")
    loginCookie.Values("locationId") = locationID
    loginCookie.Expires = Date.Now.AddDays(7)
    HttpContext.Current.Response.Cookies.Add(loginCookie)
End If

Obviously they are entirely separate functions, so I don't think the variable naming being the same is the issue.

What happens is:

  • Company Login successful > Company Cookie is Saved, User proceeds to
  • User Login User Login > User Cookie is Saved, BUT the Company Cookie is deleted.

This is using Chrome, I haven't checked other browsers but Chrome is the most important to me.

I know that this is definitely what is happening as I have checked in the Chrome Console, the Cookies are added and removed as per description above.

Can someone help point out where I am going wrong here?

EDIT - Nothing wrong with this code!

Argh... after a day of messing about with this, it turns out that the Cookie was being cleared by an unexpected, and caught, error in a different, but related section of code! This question can be closed if required, or left ...?

0

There are 0 answers