Skip to content

Commit

Permalink
Fixed issue #14. Authorization header not passed on session request.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Murphy committed Dec 13, 2011
1 parent 94c277e commit 7bf7c17
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions LoveSeat/Support/CouchBase.cs
Expand Up @@ -33,38 +33,29 @@ public static bool Authenticate(string baseUri, string userName, string password
.GetResponse();
return response.StatusCode == HttpStatusCode.OK;
}

protected Cookie GetSession()
{
public Cookie GetSession() {
var authCookie = cookiestore["authcookie"];

if (authCookie != null)
return authCookie;

if (string.IsNullOrEmpty(username)) return null;
var request = new CouchRequest(baseUri + "_session");
request.GetRequest().Headers.Add("Authorization:Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password)));
var response = request.Post()
.ContentType("application/x-www-form-urlencoded")
.Form()
.Data("name=" + username + "&password=" + password)
.GetResponse();

// add check
if (response == null)
{
throw new NullReferenceException("Response from authentication post yielded a null reference exception.");
}

var header = response.Headers.Get("Set-Cookie");
if (header != null)
{
if (header != null) {
var parts = header.Split(';')[0].Split('=');
authCookie = new Cookie(parts[0], parts[1]);
authCookie.Domain = response.Server;
cookiestore.Add("authcookie", authCookie, TimeSpan.FromMinutes(9));
}
return authCookie;
}

protected CouchRequest GetRequest(string uri)
{
return GetRequest(uri, null);
Expand Down

0 comments on commit 7bf7c17

Please sign in to comment.