Skip to content

Commit

Permalink
Improved cookie handling for the StartSessionAsync / EndSessionAsync
Browse files Browse the repository at this point in the history
[release]
  • Loading branch information
Lakritzator committed Aug 16, 2016
1 parent 9d48412 commit d922875
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions Dapplo.Jira.Shared/JiraApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,10 @@ public async Task<LoginInfo> StartSessionAsync(string username, string password,
{
throw new ArgumentNullException(nameof(password));
}
if (!_behaviour.HttpSettings.UseCookies)
{
throw new ArgumentException("Cookies need to be enabled", nameof(IHttpSettings.UseCookies));
}
var sessionUri = JiraAuthUri.AppendSegments("session");

_behaviour.MakeCurrent();
Expand All @@ -650,7 +654,6 @@ public async Task<LoginInfo> StartSessionAsync(string username, string password,

HandleErrors(response);

_behaviour.CookieContainer.Add(JiraBaseUri, new Cookie(response.Response.Session.Name, response.Response.Session.Value));
return response.Response.LoginInfo;
}

Expand All @@ -659,9 +662,11 @@ public async Task<LoginInfo> StartSessionAsync(string username, string password,
/// </summary>
public async Task EndSessionAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var sessionCookie = _behaviour.CookieContainer.GetCookies(JiraBaseUri).Cast<Cookie>().FirstOrDefault();
// Find the cookie to expire
var sessionCookies = _behaviour.CookieContainer.GetCookies(JiraBaseUri).Cast<Cookie>().ToList();

if (sessionCookie != null)
// check if a cookie was found, if not skip the end session
if (sessionCookies.Any())
{
var sessionUri = JiraAuthUri.AppendSegments("session");

Expand All @@ -672,8 +677,12 @@ public async Task EndSessionAsync(CancellationToken cancellationToken = default(
{
throw new Exception($"Status: {response.StatusCode} Message: Failed to close jira session");
}
// Remove the cookie
sessionCookie.Expired = true;
// Expire the cookie
foreach (var sessionCookie in sessionCookies)
{
sessionCookie.Expired = true;
}

}
}

Expand Down

0 comments on commit d922875

Please sign in to comment.