Roomey
Roomey

Reputation: 716

UserManager's CreateAsync method doesn't work (Identity)

My Post method

        [HttpPost]
        public async Task<IActionResult> Register()
        {
            User user = new User { Email = "[email protected]", UserName = "[email protected]" };
            var result = await _userManager.CreateAsync(user, "SS22!faasd");
            if (result.Succeeded)
            {
                // install cookie
                await _signInManager.SignInAsync(user, false);
            }
            else
            {
                foreach (var error in result.Errors)
                {
                    //how can i see my errors here
                }
            }
            return Ok();
        }

I am trying to create user with UserManager and CreateAsync method, but it doesn't work. In postman I always get 200 OK.

UserManager is connected to my database, because I can get my users with get method.

Also I dont have any cshtml files, so I cant output errors on page.

Upvotes: 0

Views: 985

Answers (2)

Arsen Khachaturyan
Arsen Khachaturyan

Reputation: 8330

In postman I always get 200 OK.

You are always getting 200 OK because at the end of a method you are returning OK() method.

In order to have some other status messages consider checking if the user is created and only then return OK() method otherwise either throw an exception, like this:

throw new ArgumentException("Wasn't able to add user")

or return some HTTPError, like this:

 return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Not able to create user");

Note: you can also throw an exception if the user already exists when you try to add a new user.


I am trying to create user with UserManager and CreateAsync method, but it doesn't work.

This is connected with your _userManager.CreateAsync methods logic. Please revisit it.

Upvotes: 1

Roomey
Roomey

Reputation: 716

To find error, I return OK(result), and see this error in postman.

Upvotes: 0

Related Questions