internal LoginView(GoogleEmailUploaderModel googleEmailUploaderModel,
                       bool wasRestarted) {
      this.googleEmailUploaderModel = googleEmailUploaderModel;
      this.StartPosition = FormStartPosition.CenterScreen;
      this.Text = Resources.GoogleEmailUploaderAppNameText;
      this.Icon = Resources.GMailIcon;
      this.MaximizeBox = false;
      this.FormBorderStyle = FormBorderStyle.FixedDialog;
      this.BackgroundImage = Resources.GoogleEmailUploaderBackgroundImage;

      this.BackColor = Color.White;
      this.Size = new Size(530, 370);

      // We have to take care of the case when the SignIn View is restarted.
      // In that case we will not display the introduction dialog but will 
      // directly jump to the sign in Dialog.
      if (!wasRestarted) {
        this.ShowInstructionsScreen();
      } else {
        this.ShowSigninScreen();
      }

      this.result = LoginViewResult.Cancel;
      this.googleEmailUploaderModel.LoadingClientsEvent +=
          new VoidDelegate(this.googleEmailUploaderModel_LoadingClientsEvent);
    }
Example #2
0
        public async Task <LoginViewResult> Login(JObject parameters)
        {
            var result   = new LoginViewResult();
            var username = parameters["Username"].ToString();
            var pwd      = parameters["Pwd"].ToString();

            var user = _userBusiness.GetUserByName("admin");

            if (user != null && user.Password == pwd)
            {
                var identity = new ClaimsIdentity(Startup.OAuthBearerOptions.AuthenticationType);

                var permissions = await _permissionBusiness.GetUserPermissions(1);

                //role
                foreach (var userRole in user.UserRoles)
                {
                    var roleName = userRole.Role.Name;
                    identity.AddClaim(new Claim(ClaimTypes.Role, roleName, ClaimValueTypes.String));
                }

                var ticket = new AuthenticationTicket(identity, CreateProperties(username));
                ticket.Properties.IssuedUtc  = DateTime.UtcNow;
                ticket.Properties.ExpiresUtc = DateTime.UtcNow.Add(TimeSpan.FromDays(1));

                var token = Startup.OAuthBearerOptions.AccessTokenFormat.Protect(ticket);
                result.Token       = token;
                result.Permissions = permissions.ToList();
                result.Status      = 0;
            }
            return(result);
        }
    void signInButton_Click(object sender, EventArgs e) {
      string login = this.emailTextBox.Text;
      string password = this.passwordTextBox.Text;

      this.errorMessageLabel.ForeColor = Color.Black;
      this.errorMessageLabel.Text = Resources.SigningInInfoText;
      this.errorMessageLabel.Refresh();

      // If captcha is displayed on the screen, then we also need to send the
      // captcha text also.
      AuthenticationResponse authResponse;
      if (this.isCaptchaEnabled) {
        string captcha = this.captchaTextBox.Text;
        authResponse =
            this.googleEmailUploaderModel.SignInCAPTCHA(
                login,
                password,
                captchaToken,
                captcha);
      } else {
        authResponse = this.googleEmailUploaderModel.SignIn(login, password);
      }

      if (authResponse.AuthenticationResult ==
          AuthenticationResultKind.Authenticated) {
        UploadResult batchUploadResult =
            this.googleEmailUploaderModel.TestEmailUpload();
        if (batchUploadResult == UploadResult.Forbidden) {
          MessageBox.Show("You are not authorized to use this feature",
                          "Forbidden");
          this.result = LoginViewResult.UploadForbidden;
          this.Close();
          return;
        }

        this.errorMessageLabel.ForeColor = Color.Black;
        this.errorMessageLabel.Text = Resources.SignedInInfoText;
        this.result = LoginViewResult.SignedIn;
        this.Close();
      } else if (authResponse.AuthenticationResult ==
          AuthenticationResultKind.CAPTCHARequired) {
        // Modify all the other labels, panels and layouts accordingly.
        this.cantSigninLinkLabel.Location = new Point(40, 436);
        this.signInButton.Location = new Point(30, 461);
        this.loginPanel.Height = 323;
        this.Height = 544;

        // Modify the error message label.
        this.errorMessageLabel.Text = Resources.CaptchaErrorMessageText;
        this.errorMessageLabel.ForeColor = Color.DarkRed;
        this.errorMessageLabel.Refresh();

        this.BackgroundImage =
            Resources.GoogleEmailUploaderWithCaptchaBackgroundImage;
        this.loginPanel.BackgroundImage =
            Resources.SignInWithCaptchaBackgroundImage;

        // There are a few cases to consider here
        // 1. When there was no captcha image present: In that case we just
        //    initialize the captchaImage label and set isCaptchaEnabled
        //    boolean to true.
        // 2. There was previously a captha image present: In this case we 
        //    refresh the previous captcha image and reinitialize it with the
        //    new image returned from the captchaURL.
        this.captchaToken = authResponse.CAPTCHAToken;
        this.captchaImage.Image = authResponse.CAPTCHAImage;
        if (this.isCaptchaEnabled) {
          this.captchaImage.Refresh();
        }
        this.captchaImage.Show();
        this.captchaTextBox.Clear();
        this.captchaTextBox.Show();
        this.captchaTextBox.TabIndex = 2;
        this.captchaInstructions.Show();

        this.isCaptchaEnabled = true;
      } else {
        if (this.isCaptchaEnabled) {
          this.captchaImage.Hide();
          this.captchaTextBox.Hide();
          this.captchaTextBox.Clear();
          this.captchaImage.Hide();

          this.loginPanel.Height = 149;
          this.Height = 370;
          this.cantSigninLinkLabel.Location = new Point(30, 262);
          this.signInButton.Location = new Point(30, 287);
          this.emailTextBox.TabIndex = 2;
          this.passwordTextBox.TabIndex = 3;
          this.BackgroundImage = Resources.GoogleEmailUploaderBackgroundImage;
          this.loginPanel.BackgroundImage = Resources.SignInBackgroundImage;
        }
        if (authResponse.AuthenticationResult ==
            AuthenticationResultKind.BadAuthentication) {
          this.errorMessageLabel.Text = Resources.SignedInTryAgainInfoText;
        } else if (authResponse.AuthenticationResult ==
            AuthenticationResultKind.ConnectionFailure) {
          this.errorMessageLabel.Text = Resources.SignedInConnectionFailureText;
        } else if (authResponse.AuthenticationResult ==
            AuthenticationResultKind.TimeOut) {
          this.errorMessageLabel.Text = Resources.SignedInTimeoutText;
        } else {
          this.errorMessageLabel.Text = Resources.SignedInUnknownText;
        }
        this.errorMessageLabel.ForeColor = Color.DarkRed;
        this.errorMessageLabel.Refresh();
      }
    }