/// <summary>
/// Signs into minus
/// </summary>
/// <param name="username">Username to sign in with.</param>
/// <param name="password">Password to sign in with</param>
/// </param>
public void SignIn(String username, String password)
{
// Get a pre-configured web client
CookieAwareWebClient client = this.CreateAndSetupWebClient();
StringBuilder data = new StringBuilder();
data.Append("username="******"&password1=").Append(password);
client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
// register the completion/error listener
client.UploadStringCompleted += delegate(object sender, UploadStringCompletedEventArgs e)
{
if (e.Error != null)
{
Debug.WriteLine("SignIn operation failed: " + e.Error.Message);
this.TriggerSignInFailed(e.Error);
#if !WINDOWS_PHONE
client.Dispose();
#endif
return;
}
SignInResult result = JsonConvert.DeserializeObject <SignInResult>(e.Result);
Debug.WriteLine("SignIn operation successful: " + result);
if (result.Success)
{
result.CookieHeaders = client.getCookieHeader(new Uri(BASE_URL));
this.TriggerSignInComplete(result);
}
else
{
this.TriggerSignInFailed(new Exception("Incorrect credentials"));
}
#if !WINDOWS_PHONE
client.Dispose();
#endif
};
// submit as an asynchronous task
try
{
ThreadPool.QueueUserWorkItem((object state) =>
{
try
{
client.UploadStringAsync(SIGN_IN_URL, "POST", data.ToString());
}
catch (WebException e)
{
Debug.WriteLine("Failed to access SignIn API: " + e.Message);
this.TriggerSignInFailed(e);
#if !WINDOWS_PHONE
client.Dispose();
#endif
}
});
}
catch (Exception e)
{
Debug.WriteLine("Failed to submit task to thread pool: " + e.Message);
this.TriggerSignInFailed(e);
#if !WINDOWS_PHONE
client.Dispose();
#endif
}
}