private async Task <string> GetUserRoles(Model.Models.User user) { var searchRequest = new UserRolesSearchRequest() { IsRolesLoadingEnabled = true, IsUsersLoadingEnabled = true, UserId = user.Id }; var roles = await _userRolesService.Get <List <Model.Models.UserRole> >(searchRequest); var rolesString = roles.Select(x => x.Role.Name); return(string.Join(", ", rolesString)); }
private async Task SetLoggedUser(List <Model.Models.User> users) { var storedUser = new Model.Models.User(); users.ForEach(user => { if (ApiService.Username == user.Username) { storedUser = user; Properties.Settings.Default.Username = ApiService.Username; Properties.Settings.Default.UserId = user.Id; } }); var userRoles = await GetUserRoles(storedUser); Properties.Settings.Default.IsAdmin = userRoles.Contains("Admin") ? true : false; }
protected override async Task <AuthenticateResult> HandleAuthenticateAsync() { if (!Request.Headers.ContainsKey("Authorization")) { return(AuthenticateResult.Fail("Missing Authorization Header")); } Model.Models.User user = null; try { var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]); var credentialBytes = Convert.FromBase64String(authHeader.Parameter); var credentials = Encoding.UTF8.GetString(credentialBytes).Split(':'); var username = credentials[0]; var password = credentials[1]; user = await _userService.Authenticate(username, password); } catch { return(AuthenticateResult.Fail("Invalid Authorization Header")); } if (user == null) { return(AuthenticateResult.Fail("Invalid Username or Password")); } var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, user.Username), new Claim(ClaimTypes.Name, user.FirstName), }; foreach (var role in user.UserRoles) { claims.Add(new Claim(ClaimTypes.Role, role.Role.Name)); } var identity = new ClaimsIdentity(claims, Scheme.Name); var principal = new ClaimsPrincipal(identity); var ticket = new AuthenticationTicket(principal, Scheme.Name); return(AuthenticateResult.Success(ticket)); }
private async void AddUserBtn_Click(object sender, System.EventArgs e) { if (!this.ValidateChildren()) { return; } var userRoles = rolesCheckBoxList.CheckedItems.Cast <Model.Models.Role>().Select(role => role.Id).ToList(); var request = new UsersUpsertRequest() { Archived = userDisabledCheckBox.Checked, ConfirmPassword = confirmPasswordTextBox.Text, Password = passwordTextBox.Text, Email = emailTextBox.Text, FirstName = firstNameTextBox.Text, LastName = lastNameTextBox.Text, Roles = userRoles, Username = usernameTextBox.Text }; Model.Models.User userEntity = null; if (!_userId.HasValue) { userEntity = await _usersService.Insert <Model.Models.User>(request); } else { userEntity = await _usersService.Update <Model.Models.User>(_userId.Value, request); } if (userEntity != null) { MessageBox.Show("Opration successfull!"); this.Close(); } }