Пример #1
0
 static public async Task <User> Login(string email, string password, bool create = false)
 {
     try
     {
         User user;
         foreach (var _user in User.AllLoggedIn)
         {
             await _user.LogOutAsync();
         }
         _RealmContext = Realm.GetInstance(GetConfig(user = await User.LoginAsync(Credentials.UsernamePassword(email, password, create), Constants.AuthServerUri)));
         _RealmContext.Write(() => _RealmContext.Add(new TrackMessage {
             Id = _RealmContext.All <TrackMessage>().Count() + 1, Text = "Login"
         }));
         return(User.Current);
     }
     catch (AuthenticationException) { Console.WriteLine($"{Constants.TAG} Erreur {(create ? "Already exists" : "Unknown Username and Password combination")}"); }
     catch (SocketException sockEx) { Console.WriteLine($"{Constants.TAG} Erreur Network error: {sockEx}"); }
     catch (WebException webEx) { Console.WriteLine($"{Constants.TAG} Erreur {nameof(Login)} {(webEx.Status == WebExceptionStatus.ConnectFailure ? $"Unable to connect to Server" : "Error trying to login")} {webEx.Message}"); }
     catch (Exception e) { Console.WriteLine($"{Constants.TAG} Erreur {nameof(Login)} {(User.Current == null ? "Error trying to login" : "Credentials accepted but then failed to open Realm")} {e.GetType().FullName} {e.Message}"); }
     return(null);
 }
Пример #2
0
        static public async Task <Realm> OpenRealm(bool createUser = false)
        {
            try
            {
                foreach (var user in User.AllLoggedIn)
                {
                    await user.LogOutAsync();
                }
                var realm = await Realm.GetInstanceAsync(GetConfig(await User.LoginAsync(Credentials.UsernamePassword(API_USERNAME, API_PASSWORD, createUser), AuthServerUri)));

                using (var transaction = realm.BeginWrite())
                {
                    realm.RemoveAll <CounterMessage>();
                    realm.Add(new CounterMessage {
                        Text = Helper.ServiceStarted ? "Already started" : "Please wait, service starting"
                    });
                    transaction.Commit();
                }
                return(realm);
            }
            catch (AuthenticationException) { Console.WriteLine($"{TAG} Erreur Unknown Username and Password combination"); }
            catch (SocketException sockEx) { Console.WriteLine($"{TAG} Erreur Network error: {sockEx}"); }
            catch (WebException webEx) { Console.WriteLine($"{TAG} Erreur {(webEx.Status == WebExceptionStatus.ConnectFailure ? $"Unable to connect to Server" : "Error trying to login")} {webEx.Message}"); }
            catch (Exception e) { Console.WriteLine($"{TAG} Erreur {(User.Current == null ? "Error trying to login" : "Credentials accepted but then failed to open Realm")} {e.GetType().FullName} {e.Message}"); }
            return(null);
        }