public async Task <string> CreateUserAsync(string username, string firstname, string lastname, string position, string telephone, int?extension, string employmentDate, string registrationDate)
        {
            string message = string.Empty;

            try
            {
                ApplicationUser newUser = new ApplicationUser()
                {
                    UserName                    = username,
                    Email                       = username,
                    EmailConfirmed              = true,
                    Title                       = Enums.Titles.Mr,
                    FirstName                   = firstname,
                    LastName                    = lastname,
                    Position                    = position,
                    DirectDial                  = telephone,
                    Extension                   = extension,
                    EmploymentDate              = CommonBehaviour.ConvertStrToDateTime(employmentDate),
                    RegistrationDate            = CommonBehaviour.ConvertStrToDateTime(registrationDate),
                    LastLogInTime               = null,
                    LastLogoutTime              = null,
                    IsLoggedIn                  = false,
                    InvalidLoginAttemptCount    = 0,
                    LastInvalidLoginAttemptTime = null,
                    Locked                      = false
                };

                string         temporaryPassword = CommonBehaviour.GenerateTempPassword();
                IdentityResult result            = await userManager.CreateAsync(newUser, CommonBehaviour.GenerateTempPassword());      // user creation

                if (result != null && result.Succeeded == true)
                {
                    SendUserCreationEmail(username, temporaryPassword);
                    message = "Success - user creation successful";
                }
                else
                {
                    string errors = string.Empty;
                    foreach (string error in result.Errors)
                    {
                        errors += error + " ";
                    }
                    message = string.Format("Error : {0}", errors);
                }
            }
            catch (Exception)
            {
                message = "Error - user creation unsuccessful";
            }
            return(message);
        }
Пример #2
0
        public async Task <string> ResetPasswordAsync(string username)
        {
            string message = string.Empty;

            try
            {
                ApplicationUser userToUpdate = userManager.FindByEmail(username);
                if (userToUpdate != null)
                {
                    string temporaryPassword  = CommonBehaviour.GenerateTempPassword();
                    string resetPasswordToken = await userManager.GeneratePasswordResetTokenAsync(userToUpdate.Id);

                    IdentityResult result = await userManager.ResetPasswordAsync(userToUpdate.Id, resetPasswordToken, temporaryPassword);

                    if (result != null && result.Succeeded == true)
                    {
                        bool wasEmailed = SendPasswordResetEmail(username, temporaryPassword);
                        if (wasEmailed)
                        {
                            message = "Success - user password reset successful and user notified via email";
                        }
                        else
                        {
                            message = string.Format("Error - user password reset successful, but unable to send the notification email to {0}. Please contact IT-support", username);
                        }
                    }
                    else
                    {
                        string errors = string.Empty;
                        foreach (string error in result.Errors)
                        {
                            errors += error + " ";
                        }
                        message = string.Format("Error : {0}", errors);
                    }
                }
                else
                {
                    // user not found
                    message = "Error - user password reset unsuccessful";
                }
            }
            catch (Exception)
            {
                message = "Error - user password reset unsuccessful - Contact IT support";
            }
            return(message);
        }