Guest User

Guest User

  • Tech Writer
  • 611
  • 127.8k

Error in c# code .Please check.

Apr 13 2020 1:02 AM
  1. public async Task<IActionResult> Register([FromBody]SignUp signUp)  
  2. {  
  3. try  
  4. {  
  5. SmartWatchUser user = await userManager.FindByEmailAsync(signUp.Email);  
  6. if (user != null)  
  7. return BadRequest("Username \"" + user.Email + "\" is already taken");  
  8. SmartWatchUser smartWatchUser = new SmartWatchUser  
  9. {  
  10. Email = signUp.Email,  
  11. FirstName = signUp.UserName?.Split(" ")?[0],  
  12. LastName = signUp.UserName?.Split(" ")?[1],  
  13. UserName = signUp.Email,  
  14. Status = UserStatus.New,  
  15. LockoutEnabled = false  
  16. };  
  17. var result = await userManager.CreateAsync(smartWatchUser, signUp.Password);  
  18. if (result.Succeeded)  
  19. {  
  20. var roleResult = await userManager.AddToRoleAsync(smartWatchUser, "AppUser");  
  21. return Ok("User Registered Successfully!");  
  22. }  
  23. else  
  24. return Ok(string.Join(",", result.Errors.Select(a=>a.Description).ToArray()));  
  25. }  
  26. catch (Exception ex)  
  27. {  
  28. //TODO logging  
  29. return StatusCode(500, ex.Message);  
  30. }  
  31. }  
This is my signup code in .net core with entityfram work code first approach. I have face this error 'Index was outside the bounds of the array'. Please solve this what's problem this code.

Answers (4)