Ejemplo n.º 1
0
        public async Task <IActionResult> CreateUser([FromBody] PayoutUser model)
        {
            try
            {
                PayoutUser payoutUser = await _userManager.FindByEmailAsync(model.Email);

                if (payoutUser == null)
                {
                    model.UserName   = model.Email;
                    model.IsDisabled = false;
                    IdentityResult chkUser = await _userManager.CreateAsync(model);

                    if (chkUser.Succeeded)
                    {
                        if (model.IsOkta == true)
                        {
                            payoutUser = _userManager.FindByEmailAsync(model.Email).Result;
                            OktaRequests.CreateUserInOkta(_optionOktaKeys.URL, _optionOktaKeys.APIKey, payoutUser);
                        }
                        else
                        {
                            string code = _userManager.GenerateEmailConfirmationTokenAsync(model).Result;
                            //code = System.Web.HttpUtility.UrlEncode(code);

                            var callbackUrl = Url.Action(
                                "ConfirmEmail", "Account",
                                new { userId = model.Id, code = code },
                                protocol: Request.Scheme);


                            string Body = string.Format(@"Please confirm your account by clicking this 
                                <a href=""{0}"">link</a>", callbackUrl);

                            Helpers.SendEmail(_optionMailCredentials, model.Email, "Payout RS - password confirmation message", Body);
                        }

                        return(Ok(chkUser));
                    }

                    foreach (var error in chkUser.Errors)
                    {
                        ModelState.AddModelError("error", error.Description);
                    }
                }
                else
                {
                    ModelState.AddModelError("error", "Username already exist");
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"error while creating User: {ex}");
                return(StatusCode((int)HttpStatusCode.InternalServerError, "error while creating User: " + ex.Message));
            }

            return(BadRequest());
        }
Ejemplo n.º 2
0
        public IActionResult ChangeOktaUserGroups(string UserName, bool IsChecked = false)
        {
            //string IdProvider = ((ClaimsIdentity)HttpContext.User.Identity).Claims.Where(x => x.Type.Contains("nameidentifier")).Select(x => x.Value).FirstOrDefault();
            Dictionary <string, string> OktaGroupsFromUser = OktaRequests.getGroupsFromUserOkta(_optionOktaKeys.URL, UserName, _optionOktaKeys.APIKey);
            Dictionary <string, string> OktaUser           = OktaRequests.GetUserFromOkta(_optionOktaKeys.URL, _optionOktaKeys.APIKey, UserName);

            if (OktaUser.Count == 0)
            {
                PayoutUser User = _userManager.FindByEmailAsync(UserName).Result;
                OktaRequests.CreateUserInOkta(_optionOktaKeys.URL, _optionOktaKeys.APIKey, User);

                OktaUser = OktaRequests.GetUserFromOkta(_optionOktaKeys.URL, _optionOktaKeys.APIKey, UserName);
            }


            foreach (KeyValuePair <string, string> UserRole in OktaGroupsFromUser)
            {
                if (!OktaRequests.RemoveUserFromGroup(_optionOktaKeys.URL, _optionOktaKeys.APIKey, UserRole.Value, OktaUser.First().Value))
                {
                    return(BadRequest());
                }
            }

            if (IsChecked)
            {
                string RS_Role   = _userManager.GetRolesAsync(_userManager.FindByEmailAsync(UserName).Result).Result.FirstOrDefault();
                string OktaGroup = OktaRequests.GetGroupIdFromOkta(_optionOktaKeys.URL, _optionOktaKeys.APIKey, RS_Role);

                if (!OktaRequests.AddUserToGroup(_optionOktaKeys.URL, _optionOktaKeys.APIKey, OktaGroup, OktaUser.First().Value))
                {
                    return(BadRequest());
                }
            }

            return(Ok());
        }