Exemplo n.º 1
0
        public async Task <IHttpActionResult> Create(CreateRegiUser model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Validate custom fields
            var fields = await _fieldStore.GetCustomUserFields();

            var errors = AccountController.ValidateCustomUserFields(model.CustomFields, fields);

            if (errors.Any())
            {
                AddErrors(errors);
                return(BadRequest(ModelState));
            }

            // Create account
            var user = new RegiAuthUser {
                UserName = model.Email, Email = model.Email
            };
            var result = await UserManager.CreateAsync(user, model.Password);

            // Any errors in UserManager (such as duplicate email or insufficient password strength)
            if (!result.Succeeded)
            {
                AddErrors(result);
                return(BadRequest(ModelState));
            }

            // Get created account
            var userModel = await UserManager.FindByEmailAsync(model.Email);

            // Store custom user data
            var fieldsTasks = fields.Where(f => model.CustomFields.Any(m => m.Name == f.Name))
                              .Select(field => new CustomUserValue
            {
                FieldId = field.Id,
                Value   = model.CustomFields.Single(f => f.Name == field.Name).Value
            }).Select(value => _fieldStore.AddFieldValueForUser(userModel, value));
            await Task.WhenAll(fieldsTasks);

            // Send an email confirmation code
            var code = await UserManager.GenerateEmailConfirmationTokenAsync(userModel.Id);

            var body = EmailContentWriter.ConfirmEmail(user.Email, code);

            try
            {
                await UserManager.SendEmailAsync(userModel.Id, EmailContentWriter.ConfirmEmailSubject, body);
            }
            catch (CouldNotSendEmailException)
            {
                return(BadRequest("Could not send email"));
            }

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> ForgotPassword(ForgotRegiPassword model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await UserManager.FindByNameAsync(model.Email);

            // User not found, but we don't want to reveal this
            if (user == null)
            {
                return(Ok());
            }

            // Email not confirmed, so can't use for reset
            if (!await UserManager.IsEmailConfirmedAsync(user.Id))
            {
                return(BadRequest("Unconfirmed Email"));
            }

            // Ensure the user hasn't had a token sent recently
            var canSendModel = await UserManager.CanSendToken(RegiTokenProvider.PasswordProvider, user);

            if (!canSendModel.CanSend)
            {
                return(BadRequest($"Please wait {Math.Ceiling(canSendModel.Remaining.TotalMinutes)} minutes"));
            }

            // Send reset code to their email. Needs to be copy and pasted into client
            var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

            var body = EmailContentWriter.ResetPassword(user.Email, code);

            try
            {
                await UserManager.SendEmailAsync(user.Id, EmailContentWriter.ResetPasswordSubject, body);
            }
            catch (Exception)
            {
                return(InternalServerError());
            }

            return(Ok());
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> ResendEmail(ResendVerificationEmail model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Get account
            var user = await UserManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                return(Unauthorized());
            }

            // Ensure the user hasn't had a token sent recently
            var canSendModel = await UserManager.CanSendToken(RegiTokenProvider.EmailProvider, user);

            if (!canSendModel.CanSend)
            {
                return(BadRequest($"Please wait {Math.Ceiling(canSendModel.Remaining.TotalMinutes)} minutes"));
            }

            // Generate a new token
            var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

            // Send an email with this link
            var body = EmailContentWriter.ConfirmEmail(user.Email, code);

            try
            {
                await UserManager.SendEmailAsync(user.Id, EmailContentWriter.ConfirmEmailSubject, body);
            }
            catch (CouldNotSendEmailException)
            {
                return(InternalServerError());
            }

            return(Ok());
        }