Click here to Skip to main content
15,885,680 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I implementing a simple login and registration in client side.

In asp.net core mvc applying the coding first approach but how to register a user and inserted a record in database??



Startup.Cs

public void ConfigureServices(IServiceCollection services)
{ 
       //adding this line
         services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddDbContext<dbcontext>(options => options.UseSqlServer(Configuration.GetConnectionString("sqlconne")));

   public void Configure(IApplicationBuilder app, IHostingEnvironment env)
   {
      //adding this line
       app.UseAuthentication(); // I think it is client side user registration??
     }


dbcontext.cs

  public class dbcontext : DbContext
    {
        public dbcontext(DbContextOptions<dbcontext> options) : base(options)
        {
        }

        public DbSet<Student> stud{ get; set; }
    }
student.cs

public class Student
    {
         [Key]
        public int StudentId { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string EmailId { get; set; }

        public string Password { get; set; }
    }

RegisterController.cs

namespace CrudWithoutEf.Controllers
{
    public class RegisterController : Controller
    {
        private readonly UserManager<dbContextClass> _userManager;

        public SignInManager<dbContextClass> _signInManager;

        public RegisterController(UserManager<dbContextClass> userManager, SignInManager<dbContextClass> signInManager)
        {
            _userManager = userManager;
            _signInManager = signInManager;
        }

        [HttpGet]
        public IActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Register(dbContextClass mb)
        {
            if (ModelState.IsValid)
            {
                //error is dbcontent class is not contain username and password //here i m writing Identity user then give an error 
                var user = new dbContextClass { EmailId = mb.EmailId, Email = mb.Password };  
                //error is dbcontent class is not contain username and password
                var result = await _userManager.CreateAsync(user, mb.Password);  
                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                }
            }
            return View(mb);
        }
    }
}


error: is dbcontent class is not contain username and password

What I have tried:

also i m writing identity user,application user but give an error??
Posted
Comments
Patrice T 18-Oct-19 3:10am    
"but give an error"
What error message ? Where ?
Member 14625950 18-Oct-19 3:39am    
@Patrice this line get an error : //error is dbcontent class is not contain username and password //here i m writing Identity user then give an error
var user = new dbContextClass { EmailId = mb.EmailId, Email = mb.Password };
//error is dbcontent class is not contain username and password
var result = await _userManager.CreateAsync(user, mb.Password);
Richard Deeming 18-Oct-19 9:48am    
Copy and paste the exact error message.
Richard Deeming 18-Oct-19 9:50am    
Also, you haven't shown the definition of the dbContextClass class.
Member 14625950 19-Oct-19 1:04am    
@Richard Deeming when i go to the cursor on EmailId then show the error message dbcontextclass does not contain definition for emailid?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900