I’m making a wrapper for a forum API, one function returns this, which is meant to provide enough information for you to set a cookie to log the user in with:
<?xml version="1.0" encoding="utf-8" ?>
<ApiResponse>
<ErrorCode>0</ErrorCode>
<ErrorDescription/>
<ResultData recordcount="1">
<Record>
<Username>Gullanian</Username>
<UserID>4</UserID>
<CookieName>WWF9sLID</CookieName>
<CookieKey>UID</CookieKey>
<CookieData>Gullanian-CD45-ZCB-D72Z-AAC6</CookieData>
<CookiePath>/scirranew/forum/</CookiePath>
<ForumPath>http://127.0.0.1/scirranew/Forum/</ForumPath>
</Record>
</ResultData>
</ApiResponse>
I’ve got as far as this:
public class WebWizCookie
{
public string Username { get; set; }
public int UserID { get; set; }
public string CookieName { get; set; }
public string CookieKey { get; set; }
public string CookieData { get; set; }
public string CookiePath { get; set; }
public string ForumPath { get; set; }
/// <summary>
/// Loads this cookie so user is logged in with this cookie data.
/// </summary>
public void LoadCookie(double MinutesExpiry)
{
HttpCookie Cookie = new HttpCookie(this.CookieName);
Cookie[this.CookieKey] = this.CookieData;
Cookie.Domain = this.CookiePath;
Cookie.Expires = DateTime.Now.AddMinutes(MinutesExpiry);
HttpContext.Current.Response.Cookies.Add(Cookie);
}
It’s returning all the values fine, but I’m not being logged into the site when I call this function. Does anyone know if I’m doing anything wrong here? Forgetting to set something, or setting it incorrectly?
Edit, printing out values
After setting the cookie, if I print out the values:
HttpContext.Current.Response.Write("Cookie[" + this.CookieKey + "] = " + this.CookieData + "<br />");
HttpContext.Current.Response.Write("Cookie.Domain = " + ConfigurationSettings.AppSettings["MasterDomainRoot"] + "<br />");
I get:
Cookie[UID] = Gullanian2-4B9B-9D5-27E2-A413
Cookie.Domain = http://localhost/ScirraNew
UID
And in google chrome the developer tools says there is a cookie:

Your
Cookie.Domainis wrong. SeeHttpCookie.Domainon MSDN – it should merely contain the domain (e.g. “localhost”) not the full path (“http://localhost/ScirraNew”). There is aPathproperty if you want to restrict a cookie to only certain parts of your site, but that is a rarely-used feature (I’ve never come across a reason to do it).By the way, you don’t need to set the
Domainproperty – by default, cookies are shared by all pages that are in the same domain. You really only need to set it if you have to share cookies between different subdomains (e.g. http://www.mysite.com and secure.mysite.com, you’d need to setDomainto “mysite.com”)