public Task <string> CreateLoginAsync(OAuthUserCreate item)
        {
            if (item == null)
            {
                return(null);
            }

            OAuthUserItem entity = new OAuthUserItem()
            {
                Email         = item.Email,
                EmailVerified = item.EmailVerified,
                Password      = item.Password,
                Providers     = item.Providers,
                Claims        = item.Claims,
                Roles         = item.Roles,
                IsActive      = true
            };

            if (!string.IsNullOrEmpty(item.Password))
            {
                entity.Salt     = _passwordService.GenerateSalt();
                entity.Password = _passwordService.CreateHash(item.Password, entity.Salt);
            }

            return(_userMongoDBRepository.CreateLoginAsync(entity));
        }
Beispiel #2
0
        public async Task <IActionResult> Index(RegisterUserViewModel model)
        {
            TempOAuthUserAccessTokenItem accessTokenItem = await _oauthUserServices.GetTempOAuthUserAccessTokenAsync(model.Provider, model.ProviderUserId);

            if (ModelState.IsValid)
            {
                if (model.IsProvisioningFromExternal == false && string.IsNullOrEmpty(model.Password))
                {
                    ModelState.AddModelError("NoPassword", "You need to provide password.");

                    return(View(model));
                }

                // create user + claims
                var userToCreate = new RegisterUserModel()
                {
                    Firstname    = model.Firstname,
                    Lastname     = model.Lastname,
                    Username     = model.Username,
                    ProfileImage = model.ProfileImage,
                    CountryCodes = model.Country,
                    Password     = model.Password,
                    Email        = model.Email
                };

                userToCreate.EmailVerified = model.IsProvisioningFromExternal;

                userToCreate.Roles = new List <string>()
                {
                    "user"
                };

                userToCreate.Claims = new List <Claim>()
                {
                    new Claim("subscriptionlevel", "FreeUser")
                };

                if (!string.IsNullOrEmpty(model.Country))
                {
                    userToCreate.Claims.Add(new Claim("country", model.Country));
                }

                if (!string.IsNullOrEmpty(model.Firstname))
                {
                    userToCreate.Claims.Add(new Claim("given_name", model.Firstname));
                }

                if (!string.IsNullOrEmpty(model.Lastname))
                {
                    userToCreate.Claims.Add(new Claim("family_name", model.Lastname));
                }

                if (!string.IsNullOrEmpty(model.Username))
                {
                    userToCreate.Claims.Add(new Claim("nickname", model.Username));
                }

                if (!string.IsNullOrEmpty(model.Email))
                {
                    userToCreate.Claims.Add(new Claim("email", model.Email));
                }

                if (!string.IsNullOrEmpty(model.ProfileImage))
                {
                    userToCreate.Claims.Add(new Claim("picture", model.ProfileImage));
                }

                // if we're provisioning a user via external login, we must add the provider &
                // user id at the provider to this user's logins
                if (model.IsProvisioningFromExternal)
                {
                    userToCreate.ProviderName      = model.Provider;
                    userToCreate.ProviderSubjectId = model.ProviderUserId;
                }

                // add it through the repository
                OAuthUserCreate oauthUserCreate = new OAuthUserCreate()
                {
                    Email         = userToCreate.Email,
                    EmailVerified = userToCreate.EmailVerified,
                    Password      = userToCreate.Password,
                    Claims        = userToCreate.Claims,
                    Roles         = userToCreate.Roles
                };

                if (!string.IsNullOrEmpty(userToCreate.ProviderName) &&
                    !string.IsNullOrEmpty(userToCreate.ProviderSubjectId) &&
                    accessTokenItem.AccessToken != null)
                {
                    oauthUserCreate.Providers = new List <Provider>()
                    {
                        new Provider()
                        {
                            ProviderName         = userToCreate.ProviderName,
                            ProviderSubjectId    = userToCreate.ProviderSubjectId,
                            AccessToken          = accessTokenItem.AccessToken,
                            AccessTokenExpiresAt = accessTokenItem.CreatedOn
                        }
                    };
                }

                userToCreate.Id = await _oauthUserServices.CreateLoginAsync(oauthUserCreate);

                if (string.IsNullOrEmpty(userToCreate.Id))
                {
                    throw new Exception($"Creating a user failed.");
                }

                if (!model.IsProvisioningFromExternal)
                {
                    // log the user in
                    await HttpContext.SignInAsync(userToCreate.Id, userToCreate.Claims.ToArray());
                }

                await _qeueMessageServices.SendCreateUser(userToCreate);

                // continue with the flow
                if (_interaction.IsValidReturnUrl(model.ReturnUrl) || Url.IsLocalUrl(model.ReturnUrl))
                {
                    return(Redirect(model.ReturnUrl));
                }

                return(Redirect("~/"));
            }

            // ModelState invalid, return the view with the passed-in model
            // so changes can be made
            return(View(model));
        }