Example #1
0
        public async Task <IActionResult> Edit(UserModel user)
        {
            try
            {
                var usernamelower = user.Username.ToLower();
                if (_db.User.Any(d => d.Username.ToLower() == usernamelower && d.UserId != user.Id))
                {
                    return(BadRequest(new { Success = false, Error = "A record with this name already exists" }));
                }

                var entity = _db.User.FirstOrDefault(d => d.UserId == user.Id);
                entity.Fullname = user.Fullname;
                if (!string.IsNullOrWhiteSpace(user.Password))
                {
                    entity.Password = _hasher.GenerateIdentityV3Hash(user.Password);
                }
                entity.Username = user.Username;
                entity.RoleId   = user.RoleId;
                await _db.SaveChangesAsync();

                return(Ok(new { Success = true }));
            }
            catch (Exception e)
            {
                _logger.logError(e);
            }
            return(BadRequest(new { Success = false }));
        }
Example #2
0
        public async Task <RegistrationResponse> RegisterAsync(string fullName, string username, string password, int roleid)
        {
            username = username.ToLower();
            var existing = _db.User.AsQueryable().FirstOrDefault(d => d.Username == username);

            if (existing != null)
            {
                return(new RegistrationResponse
                {
                    Errors = new List <string>
                    {
                        "A user with this username already exists",
                    }
                });
            }
            password = _hasher.GenerateIdentityV3Hash(password);
            var user = new User
            {
                Fullname = fullName,
                Username = username,
                Password = password,
                RoleId   = roleid
            };

            _db.User.Add(user);
            await _db.SaveChangesAsync();

            var token = await GenerateToken(user);

            return(new RegistrationResponse
            {
                Success = true,
                Token = token.Item1,
                RefreshToken = token.Item2
            });
        }
        public static Task <SignUpResult> Show(Window parent)
        {
            var title  = "Create Account";
            var msgbox = new SignUpWindow
            {
                Title = title
            };
            var buttonPanel = msgbox.FindControl <StackPanel>("Buttons");

            SignUpResult res = new SignUpResult();

            void AddButton(string caption)
            {
                var btn = new Button {
                    Content = caption
                };

                btn.Click += (_, __) =>
                {
                    var nickName     = msgbox.FindControl <TextBox>("tbNickName").Text;
                    var passwordHash = msgbox.FindControl <TextBox>("tbPassword").Text;
                    var email        = msgbox.FindControl <TextBox>("tbEmail").Text;
                    var firstName    = msgbox.FindControl <TextBox>("tbFirstName").Text;
                    var lastName     = msgbox.FindControl <TextBox>("tbLastName").Text;
                    if (string.IsNullOrWhiteSpace(nickName))
                    {
                        ShowError("Incorrect Nick name");
                        return;
                    }
                    if (string.IsNullOrWhiteSpace(passwordHash) || passwordHash.Length < 6)
                    {
                        ShowError("Incorrect Password");
                        return;
                    }
                    if (string.IsNullOrWhiteSpace(firstName))
                    {
                        ShowError("Incorrect First name");
                        return;
                    }
                    if (string.IsNullOrWhiteSpace(lastName))
                    {
                        ShowError("Incorrect Last name");
                        return;
                    }
                    if (string.IsNullOrWhiteSpace(email) || !email.Contains('@') || !email.Contains('.'))
                    {
                        ShowError("Incorrect Email");
                        return;
                    }

                    PasswordHasher hasher = new PasswordHasher();
                    passwordHash = hasher.GenerateIdentityV3Hash(passwordHash);

                    res = new SignUpResult
                    {
                        Email        = email,
                        PasswordHash = passwordHash,
                        Id           = 1,
                        Time         = DateTime.Now.ToString()
                    };

                    var path = AppDomain.CurrentDomain.BaseDirectory + "user_info";
                    if (File.Exists(path))
                    {
                        ShowInfo("You already signed up. Please log in.");
                        msgbox.Close();
                        return;
                    }

                    res.IsSignIn = true;

                    File.AppendAllText(
                        path,
                        JsonConvert.SerializeObject(res));
                    msgbox.Close();
                };
                buttonPanel.Children.Add(btn);
            }

            AddButton("Sign Up");
            var tcs = new TaskCompletionSource <SignUpResult>();

            msgbox.Closed += delegate { tcs.TrySetResult(res); };
            if (parent != null)
            {
                msgbox.ShowDialog(parent);
            }
            else
            {
                msgbox.Show();
            }
            return(tcs.Task);
        }