Forums - C# Corner

Forum guidelines

Registering New Users

Jun 19 2017 4:13 PM
Hello,
 
I'm adding the functionality of adding new users to my project following this project:
 
http://www.c-sharpcorner.com/article/asp-net-core-mvc-authentication-and-role-based-authorization-with-asp-net-core/
 
However, I encounter a problem when I try to Add a new user:
 
On the Post Method of the UserController: 
  1. [HttpPost]  
  2.         public async Task<IActionResult> AddUser(UserViewModel model)  
  3.         {  
  4.             if (ModelState.IsValid)  
  5.             {  
  6.                 ApplicationUser user = new ApplicationUser  
  7.                 {  
  8.                     Name = model.Name,  
  9.                     UserName = model.UserName,  
  10.                     Email = model.Email  
  11.                 };  
  12.                   
  13.                 IdentityResult result = await userManager.CreateAsync(user, model.Password);  
  14.                   
  15.                 if (result.Succeeded)  
  16.                 {  
  17.                     ApplicationRole applicationRole = await roleManager.FindByIdAsync(model.ApplicationRoleId);  
  18.                     if (applicationRole != null)  
  19.                     {  
  20.                         IdentityResult roleResult = await userManager.AddToRoleAsync(user, applicationRole.Name);  
  21.                         if (roleResult.Succeeded)  
  22.                         {  
  23.                             return RedirectToAction("Index");  
  24.                         }  
  25.                     }  
  26.                 }  
  27.             }  
  28.             return View(model);  
  29.         }  
 I'm getting an error on this line:
 
  1. IdentityResult result = await userManager.CreateAsync(user, model.Password);    
  2.                     
  3.                 if (result.Succeeded)    
When I put a break here, I see that result is null.
 
 
 
 I'm trying to solve it on my own but no luck so far.
 
Any ideas? Thanks in advance for any help.
 
Regards, 
 

Answers (4)