osyris zosar

osyris zosar

  • NA
  • 289
  • 26.8k

Creating Login System

Mar 14 2021 3:51 AM
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:  
  1. public void ConfigureServices(IServiceCollection services)  
  2.         {  
  3.             services.AddIdentity<AppUser, AppRole>(options =>  
  4.              {  
  5.                  options.User.RequireUniqueEmail = true;  
  6.   
  7.   
  8.              }).AddEntityFrameworkStores<IdentityAppContext>()  
  9.              .AddDefaultTokenProviders();  
  10.   
  11.             services.ConfigureApplicationCookie(config =>  
  12.            {  
  13.                config.Cookie.Name = "Login_Cookie1";  
  14.                config.LoginPath = "/Account/login";  
  15.            });  
  16.   
  17.             services.AddDbContext<IdentityAppContext>(options =>  
  18.             options.UseSqlServer(Configuration.GetConnectionString("Master")));  
  19.   
  20.             services.AddControllersWithViews();  
  21.         }  
 
 appsettings.json:
  1. "ConnectionStrings": {  
  2.     "Master""Server=LAPTOP-VI29TVE0;Database=MasterDb;Trusted_Connection=True;MultipleActiveResultSets=true"  
  3.   }  
 
Controller:
  1. public class AccountController : Controller  
  2.     {  
  3.         private UserManager<AppUser> Usermgr { get; }  
  4.         private SignInManager<AppUser> SignInmgr { get; }  
  5.         public AccountController(UserManager<AppUser> userManager,  
  6.             SignInManager<AppUser> signInManager)  
  7.         {  
  8.             Usermgr = userManager;  
  9.             SignInmgr = signInManager;  
  10.         }  
  11.   
  12.         public async Task<IActionResult> Register()  
  13.         {  
  14.   
  15.             var Person = new AppUser();  
  16.             Person.UserName = "username1";  
  17.             Person.Email = "[email protected]";  
  18.             Person.FirstName = "Firstname1";  
  19.             Person.LastName = "chees123";  
  20.   
  21.             IdentityResult result = await Usermgr.CreateAsync(Person, "Password");  
  22.   
  23.             if (result.Succeeded)  
  24.             {  
  25.                 ViewBag.Message = "Account was succesfully created";  
  26.   
  27.             }  
  28.             else  
  29.             {  
  30.                 ViewBag.Message = "Something went wrong";  
  31.             }  
  32. return View();  
  33.         }  
  34. }  
 IdentiyAppContext:
 
  1. public class IdentityAppContext : IdentityDbContext<AppUser,AppRole, int>  
  2.       
  3. {  
  4.     public IdentityAppContext(DbContextOptions<IdentityAppContext> options)  
  5.         : base(options)  
  6.     {   
  7.       
  8.     }  
  9.   
  10. }  
 AppUser:
 
  1. public class AppUser : IdentityUser<int>  
  2.   
  3. {  
  4.     public string FirstName { get; set; }  
  5.     public string LastName { get; set; }  
  6. }  
 Approle is empty at the moment:
  1. public class AppRole : IdentityRole<int//Identity calm  
  2. {  
  3.   
  4. }  
 Register view page: 
  1. @{   
  2.     ViewData["Title"] = "Register";  
  3. }  
  4.   
  5. <h2>Register</h2>  
  6. <p>@ViewBag.Message</p>  
  7. <div>  
  8.     <a asp-controller="Account" asp-action="Login">Login</a>  
  9. </div>  
 it does not add to the database and i keep geting the message : "Something went wrong" (from the viewbag)

Answers (2)