Exemplo n.º 1
0
 private static async Task CreateUserIfNotExist(UserManager<ApplicationUser> userManager, string email, string password, string role, string loginProvider = null, string providerKey = null)
 {
     var user = await userManager.FindByEmailAsync(email);
     if (user == null)
     {
         user = new ApplicationUser {UserName = email, Email = email};
         var result = await userManager.CreateAsync(user, password);
         if (!result.Succeeded)
         {
             throw new ApplicationException(string.Join("\n", result.Errors.Select(a => a.Description).ToArray()));
         }
         await userManager.AddToRoleAsync(user, role);
         if (loginProvider != null && providerKey != null)
         {
             await userManager.AddLoginAsync(user, new UserLoginInfo(loginProvider, providerKey, ""));
         }
     }
 }
Exemplo n.º 2
0
 private static async Task<ApplicationUser> CreateUserIfNotExist(UserManager<ApplicationUser> userManager, ApplicationUser user, string password, string role, string loginProvider = null, string providerKey = null)
 {
     //Debugger.Launch();
     user.EmailConfirmed = true;
     user.Email = user.Email ?? user.UserName;
     if (await userManager.FindByEmailAsync(user.Email) == null)
     {
         var result = await userManager.CreateAsync(user, password);
         if (!result.Succeeded)
         {
             throw new ApplicationException(string.Join("\n", result.Errors.Select(a => a.Description).ToArray()));
         }
         await userManager.AddToRoleAsync(user, role);
         if (loginProvider != null && providerKey != null)
         {
             await userManager.AddLoginAsync(user, new UserLoginInfo(loginProvider, providerKey, ""));
         }
     }
     return user;
 }
Exemplo n.º 3
0
 public async Task<IActionResult> Create(ApplicationUser user)
 {
     if (ModelState.IsValid)
     {
         var result = await _userManager.CreateAsync(user);
         if (result.Succeeded)
         {
             if (!user.EmailConfirmed)
             {
                 var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                 var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, confirmCode = code }, protocol: HttpContext.Request.Scheme);
                 var body = await _emailTemplate.RenderViewToString(@"/Views/Email/ActivateEmail", new ActivateEmail() { Emailaddress = user.Email, Callback = callbackUrl });
                 await _emailSender.SendEmailAsync(user.Email, T["Confirm your account"], body);
                 return new JsonResult(new { success = "dbc.snackbar", data = "An Email is send to user" });
             }
             return new EmptyResult();
         }
         else
         {
             AddErrors(result);
         }
     }
     return PartialView(user);
 }
Exemplo n.º 4
0
 public async Task<IActionResult> Delete(ApplicationUser model)
 {
     var user = await _userManager.FindByIdAsync(model.Id);
     await _userManager.DeleteAsync(user);
     return new EmptyResult();
 }
Exemplo n.º 5
0
        public async Task<IActionResult> Edit(ApplicationUser model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByIdAsync(model.Id);
                user.AccessFailedCount = model.AccessFailedCount;
                user.Email = model.Email;
                user.EmailConfirmed = model.EmailConfirmed;
                user.LockoutEnabled = model.LockoutEnabled;
                user.LockoutEnd = model.LockoutEnd;
                user.PhoneNumber = model.PhoneNumber;
                user.PhoneNumberConfirmed = model.PhoneNumberConfirmed;
                user.TwoFactorEnabled = model.TwoFactorEnabled;
                user.UserName = model.UserName;
                await _userManager.UpdateAsync(user);

                DbContext.SaveChanges();
                return new EmptyResult();
            }
            return PartialView(model);
        }