protected void DeleteToken() { using (var context = ContextFactory()) { context.ObjectTrackingEnabled = true; var original = context.Tokens.FirstOrDefault(t => t.Network == _network); if (original == null) return; context.Tokens.DeleteOnSubmit(original); context.SubmitChanges(); } Token = null; }
public async Task<Token> LoginAsync(Token token) { // Check for network connectivity if (!NetworkInterface.GetIsNetworkAvailable()) return null; var client = new RestClient("http://trainshare.herokuapp.com/v1/"); var request = new RestRequest("login", Method.POST) .AddJson( new JObject( new JProperty("network", token.Network), new JProperty("access_token", token.AccessToken), new JProperty("access_token_secret", token.AccessTokenSecret))); request.RequestFormat = DataFormat.Json; try { var json = await client .ExecutTaskAsync(request) .ContinueWith(task => JObject.Parse(task.Result.Content)); InsertOrUpdateToken( new Token { Network = "trainshare", AccessToken = json["trainshare_id"].Value<string>(), AccessTokenSecret = json["trainshare_token"].Value<string>() }); Events.Publish(Token); return Token; } catch (Exception e) { _logger.Error(e); return null; } }
protected void InsertOrUpdateToken(Token token) { using (var context = ContextFactory()) { context.ObjectTrackingEnabled = true; var original = context.Tokens.FirstOrDefault(t => t.Network == _network); if (original == null) context.Tokens.InsertOnSubmit(token); else { context.Tokens.DeleteOnSubmit(original); context.Tokens.InsertOnSubmit(token); } context.SubmitChanges(); } Token = token; }
protected void ReloadToken() { using (var context = ContextFactory()) { Token = context.Tokens.FirstOrDefault(t => t.Network == _network); } }