Exemplo n.º 1
0
        public async void UpdatePassword(CetUser user, string newPassword)
        {
            string hashed = HashPassword(newPassword);

            user.Password = hashed;

            _context.Update(user);

            await _context.SaveChangesAsync();
        }
Exemplo n.º 2
0
        private void registerButton_Click(object sender, RoutedEventArgs e)
        {
            string name          = nameTextBox.Text;
            string surname       = surnameTextBox.Text;
            string username      = usernameTextBox.Text;
            string password      = passwordTextBox.Password;
            string passwordAgain = passwordAgainTextBox.Password;
            Role   role          = (Role)roleComboBox.SelectionBoxItem;

            if (password != passwordAgain)
            {
                MessageBox.Show("Parolalar eşleşmiyor.");

                return;
            }

            CetUserService service = ServiceProvider.GetUserService();

            CetUser user = new CetUser
            {
                Name        = name,
                Surname     = surname,
                UserName    = username,
                Role        = role,
                Password    = CetUserService.HashPassword(password),
                CreatedDate = DateTime.Now
            };

            var errors = DataValidator.Validate(user).ToList();

            if (errors.Any())
            {
                StringBuilder errorText = new StringBuilder();

                foreach (DataValidator.ErrorInfo errorInfo in errors)
                {
                    errorText.Append(errorInfo.Property + " is not valid.\r\n");
                    errorText.Append(errorInfo.Message + "\r\n\r\n");
                }

                MessageBox.Show(errorText.ToString());

                return;
            }

            service.Register(user);

            MainWindow window = new MainWindow(user);

            window.Show();

            this.Close();

            MessageBox.Show("Register Successful");
        }
Exemplo n.º 3
0
        public async Task <IList <string> > AssignRoleToUser(string userName, string role)
        {
            CetUser user = GetByUserName(userName);

            if (user != null)
            {
                var result = await _userManager.AddToRoleAsync(user, role);

                return(await _userManager.GetRolesAsync(user));
            }
            return(null);
        }
Exemplo n.º 4
0
        private async Task LoadAsync(CetUser user)
        {
            var email = await _userManager.GetEmailAsync(user);

            Email = email;

            Input = new InputModel
            {
                NewEmail = email,
            };

            IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user);
        }
Exemplo n.º 5
0
        private async Task LoadAsync(CetUser user)
        {
            var userName = await _userManager.GetUserNameAsync(user);

            var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

            Username = userName;

            Input = new InputModel
            {
                PhoneNumber = phoneNumber,
                Name        = user.Name,
                Surname     = user.Surname,
            };
        }
Exemplo n.º 6
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new CetUser {
                    UserName = Input.Email, Email = Input.Email, Name = Input.Name, Surname = Input.Surname
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
        private async Task LoadSharedKeyAndQrCodeUriAsync(CetUser user)
        {
            // Load the authenticator key & QR code URI to display on the form
            var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);

            if (string.IsNullOrEmpty(unformattedKey))
            {
                await _userManager.ResetAuthenticatorKeyAsync(user);

                unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            }

            SharedKey = FormatKey(unformattedKey);

            var email = await _userManager.GetEmailAsync(user);

            AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey);
        }
Exemplo n.º 8
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Dış giriş bilgisi onayı yüklenirken hata oluştu.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                var user = new CetUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation("Kullanıcı şu sağlayıcıyı kullanarak hesap oluşturdu: {Name}.", info.LoginProvider);
                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }
Exemplo n.º 9
0
        public bool Signup(string userName, string password, string email, string firstName, string lastName)
        {
            var user = new CetUser
            {
                UserName  = userName,
                Email     = email,
                FirstName = firstName,
                LastName  = lastName
            };
            var result = _userManager.CreateAsync(user, password);

            if (result.Result.Succeeded)
            {
                _userManager.AddToRoleAsync(user, "User");
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 10
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new CetUser {
                    UserName = Input.Email, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, BirthDate = Input.BirthDate
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new CetUser {
                    UserName = Input.Email, Email = Input.Email, City = Input.City, FirstName = Input.FirstName, LastName = Input.LastName, SchoolNo = Input.SchoolNo, Department = Input.Department
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("Kullanıcı şifre ile yeni hesap oluşturdu.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Hesabınızı şu şekilde doğrulayın: <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>buraya tıklayınız</a>.");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
        public UpdatePasswordWindow(CetUser user)
        {
            InitializeComponent();

            this._user = user;
        }
Exemplo n.º 13
0
 public bool IsPasswordTrue(CetUser user, string password)
 {
     return(_context.CetUsers
            .Where(cetUser => cetUser.Id == user.Id)
            .Any(cetUser => cetUser.Password == HashPassword(password)));
 }
Exemplo n.º 14
0
        public async void Register(CetUser user)
        {
            await _context.AddAsync(user);

            await _context.SaveChangesAsync();
        }
Exemplo n.º 15
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                var user = new CetUser {
                    UserName = Input.Email, Email = Input.Email
                };

                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);

                        var userId = await _userManager.GetUserIdAsync(user);

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                        var callbackUrl = Url.Page(
                            "/Account/ConfirmEmail",
                            pageHandler: null,
                            values: new { area = "Identity", userId = userId, code = code },
                            protocol: Request.Scheme);

                        await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                          $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                        // If account confirmation is required, we need to show the link if we don't have a real email sender
                        if (_userManager.Options.SignIn.RequireConfirmedAccount)
                        {
                            return(RedirectToPage("./RegisterConfirmation", new { Email = Input.Email }));
                        }

                        await _signInManager.SignInAsync(user, isPersistent : false, info.LoginProvider);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            ProviderDisplayName = info.ProviderDisplayName;
            ReturnUrl           = returnUrl;
            return(Page());
        }
Exemplo n.º 16
0
        public IEnumerable <string> GetUserRoles(string userName)
        {
            CetUser user = GetByUserName(userName);

            return(_userManager.GetRolesAsync(user).Result);
        }
Exemplo n.º 17
0
 public void Add(CetUser user)
 {
     _context.Add(user);
     _context.SaveChanges();
 }
Exemplo n.º 18
0
 public void Update(CetUser user)
 {
     _context.Update(user);
     _context.SaveChanges();
 }
Exemplo n.º 19
0
 public void Remove(CetUser user)
 {
     _context.Remove(user);
     _context.SaveChanges();
 }
Exemplo n.º 20
0
        public MainWindow(CetUser cetUser)
        {
            InitializeComponent();

            _loginUser = cetUser;
        }