简体   繁体   中英

Name cannot be null or empty: _userManager.CreateAsync

I'm getting this error:

Name cannot be null or empty

while making a POST call: https://localhost:44346/api/account/register . _userManager.CreateAsync is not liking it for some reason.

My code:

UserModel.cs class:

public class UserModel : IdentityUser
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

AccountController.cs:

// POST api/Account/Register
        [AllowAnonymous]
        [Route("Register")]
        public async Task<IHttpActionResult> Register(UserModel userModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            IdentityResult result = await _repo.RegisterUser(userModel);

            IHttpActionResult errorResult = GetErrorResult(result);

            if (errorResult != null)
            {
                return errorResult;
            }

            return Ok();
        }

AuthRepository class where RegisterUser is implemented:

private AuthContext _ctx;

        private UserManager<UserModel> _userManager;

        public AuthRepository()
        {
            _ctx = new AuthContext();
            _userManager = new UserManager<UserModel>(new UserStore<UserModel>(_ctx));
        }
public async Task<IdentityResult> RegisterUser(UserModel userModel)
        {
            UserModel user = new UserModel
            {
                UserName = userModel.UserName
            };

            try
            {
                var result = await _userManager.CreateAsync(user, userModel.Password);

                return result;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

        }

What I dont understand is why am I getting this error "Name cannot be null" when I am clearly assigning it.

What I have tried so far: 1. Adding new property Name in my UserModel.cs 2. Removing UserName from UserModel class as suggested here Name cannot be null or empty. asp.net identity mvc 5 + EntityFreamwork but that gives me a different exception (UserModel does not exist in the context).

I am trying to implement OAuth as suggested here: http://bitoftech.net/2014/07/16/enable-oauth-refresh-tokens-angularjs-app-using-asp-net-web-api-2-owin/ His source code: https://github.com/tjoudeh/AngularJSAuthentication

Please help.

Edit: I see 2 UserName properties for some reason and 1 of it is null. Is that the reason why I am seeing an error?

在此处输入图片说明

使用ADO.NET连接字符串代替EntityFramework之一,此问题将自行解决。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM