public virtual async Task <IdentityContactResult> CreateUser(SiteUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            if (user.Password.IsNullOrEmpty())
            {
                throw new MissingFieldException("Password");
            }

            if (user.Email.IsNullOrEmpty())
            {
                throw new MissingFieldException("Email");
            }

            var result = new IdentityContactResult();

            if ((await UserManager().FindByEmailAsync(user.Email)) != null)
            {
                result.IdentityResult = IdentityResult.Failed(new IdentityError {
                    Description = _localizationService.GetString("/Registration/Form/Error/UsedEmail", "This email address is already used")
                });
            }
            else
            {
                result.IdentityResult = await UserManager().CreateAsync(user, user.Password);

                if (result.IdentityResult.Succeeded)
                {
                    var identity = await SignInManager().GenerateUserIdentityAsync(user);
                    await SignInManager().SignInWithClaimsAsync(user, true, identity.Claims);

                    result.FoundationContact = CreateFoundationContact(user);
                }
            }

            return(result);
        }
Example #2
0
        public virtual async Task <IdentityContactResult> CreateUser(SiteUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            if (user.Password.IsNullOrEmpty())
            {
                throw new MissingFieldException("Password");
            }

            if (user.Email.IsNullOrEmpty())
            {
                throw new MissingFieldException("Email");
            }

            var result = new IdentityContactResult();

            if (UserManager().FindByEmail(user.Email) != null)
            {
                result.IdentityResult = new IdentityResult(_localizationService.GetString("/Registration/Form/Error/UsedEmail", "This email address is already used"));
            }
            else
            {
                result.IdentityResult = await UserManager().CreateAsync(user, user.Password);

                if (result.IdentityResult.Succeeded)
                {
                    var identity = await UserManager().GenerateUserIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

                    AuthenticationManager().SignIn(identity);
                    result.FoundationContact = CreateFoundationContact(user);
                }
            }

            return(result);
        }