2

I created Asp.Net Core 3.0 web Api project with Identity model (Identity.EntityFrameworkCore Version=3.1.3.0), Now I can Login and Register using api controller, But I have another project created by Asp.Net MVC 5 (Database First) which connecting on the same database, I need in MVC Project adding users in AspNetUsers table directly in the shared database without using Web Api project.

[HttpPost]  
[ValidateAntiForgeryToken]  
public ActionResult AddUser(UserViewModel newUser)  
{  
   if (ModelState.IsValid)  
    {
      // How to add user manually here
     _dbContext.AspNetUsers.Add(newUser);  
     _dbContext.SaveChanges();  
    }  
    return View();  
}  
2
  • 2
    Is there a reason why you can't have the MVC call the API and have it add the user? All the MVC would need is the bearer token then. I don't see the issue though with the above code. If it is in the MVC project, and you are inside of it, then all you would need to do is convert your UserViewModel to be an object that the AspNetUsers table expects. If it is an issue of dbContext not seeing the AspNetUsers table, then I would suggest seeing where the Database first creates its definition of tables and manually add that in there Commented Apr 14, 2020 at 2:37
  • 1
    @SomeStudent The issue I can't create the object that the AspNetUsers table expects because I don't know how correctly adding password in PasswordHash field without UserManager Commented Apr 14, 2020 at 8:49

1 Answer 1

1

you must use [Body] annotation for input model. and use a mapper (ex AutoMapper) to map your ViewModel to Db Model. in IApplicationUserManager method CreateAsync(User user, string password) create user.

var user = new User
{
     UserName = newUser.UserName,
     Email = newUser.Email
};
var result = await _userManager.CreateAsync(user, model.Password).ConfigureAwait(false);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.