Exemplo n.º 1
0
        public async Task Login(object parameter)
        {
            try
            {
                Failure       = false;
                FailureReason = "";

                var passwordCtrl = parameter as PasswordBox;

                if (passwordCtrl == null)
                {
                    throw new Exception("can not find the password control");
                }

                Password = passwordCtrl.Password;

                if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
                {
                    Failure       = true;
                    FailureReason = "user name and password is requried";
                    return;
                }

                CanExecute = false;

                ApiConsumer consumer = new ApiConsumer();
                var         result   = await consumer.LoginAsync(UserName, Password);

                if (result.StatusCode == HttpStatusCode.OK)
                {
                    Failure       = false;
                    FailureReason = "";

                    var token = new AuthToken()
                    {
                        Key       = result.JContent["access_token"].Value <string>(),
                        ExpiresIn = result.JContent["expires_in"].Value <int>(),
                        TokenType = result.JContent["token_type"].Value <string>()
                    };

                    AppContext.Current.AuthToken            = token;
                    AppContext.Current.App.CurPageViewModel = new SendImageModel();
                }
                else
                {
                    Failure       = true;
                    FailureReason = "";
                    foreach (var cur in result.JContent)
                    {
                        FailureReason += cur.Value.Value <string>() + " ";
                    }
                    FailureReason = FailureReason.TrimEnd(' ');
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);

                Failure       = true;
                FailureReason = ex.Message;
            }
            finally
            {
                CanExecute = true;
            }
        }