public void LoginInBackground(string authenticationToken, Action <MobileServiceUser, Exception> continueWith) { // Proper Async Tasks Programming cannot integrate with Windows Phone (stupid) Async Mechanisim which use Events... (ex: UploadStringCompleted) //var asyncTask = new Task<MobileServiceUser>(() => this.StartLoginAsync(authenticationToken)); //asyncTask.Start(); //return asyncTask; if (authenticationToken == null) { throw new ArgumentNullException("authenticationToken"); } if (string.IsNullOrEmpty(authenticationToken)) { throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, "Error! Empty Argument: {0}", "authenticationToken")); } var client = _clientFactory.GetClient(); var url = _serviceUrl + LoginAsyncUriFragment + "?mode=" + LoginAsyncAuthenticationTokenKey; client.Headers[HttpRequestHeader.ContentType] = RequestJsonContentType; var payload = new JObject(); payload[LoginAsyncAuthenticationTokenKey] = authenticationToken; client.UploadStringCompleted += (x, args) => { if (args.Error != null) { var ex = args.Error; if (args.Error.InnerException != null) { ex = args.Error.InnerException; } continueWith(null, ex); return; } var result = JObject.Parse(args.Result); CurrentAuthToken = result["authenticationToken"].Value <string>(); CurrentUser = new MobileServiceUser(result["user"]["userId"].Value <string>()); continueWith(CurrentUser, null); }; //Go! client.UploadStringAsync(new Uri(url), payload.ToString()); }
public void Logout() { CurrentUser = null; CurrentAuthToken = null; }