Advertisement
onzulin

registro de usuarios asp.net core

Aug 29th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. public async Task<IActionResult> OnPostAsync(string returnUrl = null)
  2.         {
  3.             returnUrl = returnUrl ?? Url.Content("~/");
  4.             if (ModelState.IsValid)
  5.             {
  6.                 var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email };
  7.                 var result = await _userManager.CreateAsync(user, Input.Password);
  8.                 if (result.Succeeded)
  9.                 {
  10.                     _logger.LogInformation("User created a new account with password.");
  11.  
  12.                     var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
  13.                     var callbackUrl = Url.Page(
  14.                         "/Account/ConfirmEmail",
  15.                         pageHandler: null,
  16.                         values: new { userId = user.Id, code = code },
  17.                         protocol: Request.Scheme);
  18.  
  19.                     await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
  20.                         $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
  21.  
  22.                     await _signInManager.SignInAsync(user, isPersistent: false);
  23.                    
  24.                    
  25.  
  26.                 }
  27.                 foreach (var error in result.Errors)
  28.                 {
  29.                     ModelState.AddModelError(string.Empty, error.Description);
  30.                 }
  31.                 //Quiero añadir el rol del usuario si no hay usuarios SuperAdmin si hay usuarios sera User.
  32.                 //método que devuelva true o false solo necesito escribir el usuario y el rol que tiene en la tabla
  33.                 LUsuarios LUsers = new LUsuarios();
  34.                 int countUsers = await LUsers.CountUsers();
  35.                 if (countUsers == 0)
  36.                 {
  37.                     var roleUser = new ApplicationUserRole { UserId = user.Id, RoleId = 1 };
  38.                 }
  39.                 else
  40.                 {
  41.                     var roleUser0 = new ApplicationUserRole { UserId = user.Id, RoleId = 4 };
  42.                 }
  43.                
  44.             }
  45.             // If we got this far, something failed, redisplay form
  46.             return Page();
  47.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement