I am trying to create my own login system but for some reason it wont add a new user to my MSSMS database
i have already created the migration and the tables are in the database
Startup Code:
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddIdentity<AppUser, AppRole>(options =>
- {
- options.User.RequireUniqueEmail = true;
-
-
- }).AddEntityFrameworkStores<IdentityAppContext>()
- .AddDefaultTokenProviders();
-
- services.ConfigureApplicationCookie(config =>
- {
- config.Cookie.Name = "Login_Cookie1";
- config.LoginPath = "/Account/login";
- });
-
- services.AddDbContext<IdentityAppContext>(options =>
- options.UseSqlServer(Configuration.GetConnectionString("Master")));
-
- services.AddControllersWithViews();
- }
appsettings.json:
- "ConnectionStrings": {
- "Master": "Server=LAPTOP-VI29TVE0;Database=MasterDb;Trusted_Connection=True;MultipleActiveResultSets=true"
- }
Controller:
- public class AccountController : Controller
- {
- private UserManager<AppUser> Usermgr { get; }
- private SignInManager<AppUser> SignInmgr { get; }
- public AccountController(UserManager<AppUser> userManager,
- SignInManager<AppUser> signInManager)
- {
- Usermgr = userManager;
- SignInmgr = signInManager;
- }
-
- public async Task<IActionResult> Register()
- {
-
- var Person = new AppUser();
- Person.UserName = "username1";
- Person.Email = "[email protected]";
- Person.FirstName = "Firstname1";
- Person.LastName = "chees123";
-
- IdentityResult result = await Usermgr.CreateAsync(Person, "Password");
-
- if (result.Succeeded)
- {
- ViewBag.Message = "Account was succesfully created";
-
- }
- else
- {
- ViewBag.Message = "Something went wrong";
- }
- return View();
- }
- }
IdentiyAppContext:
- public class IdentityAppContext : IdentityDbContext<AppUser,AppRole, int>
-
- {
- public IdentityAppContext(DbContextOptions<IdentityAppContext> options)
- : base(options)
- {
-
- }
-
- }
AppUser:
- public class AppUser : IdentityUser<int>
-
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- }
Approle is empty at the moment:
- public class AppRole : IdentityRole<int>
- {
-
- }
Register view page:
- @{
- ViewData["Title"] = "Register";
- }
-
- <h2>Register</h2>
- <p>@ViewBag.Message</p>
- <div>
- <a asp-controller="Account" asp-action="Login">Login</a>
- </div>
it does not add to the database and i keep geting the message : "Something went wrong" (from the viewbag)