How to register New Users as Students? (MVC)

asp.net-mvc c# entity-framework-6

Question

I am trying to learn to use Authentication & Identity Security in MVC and Entity Framework and having some difficulty.

I am working on a University Web App and the issue I am having is connecting the new Users who will be registering in the App as different roles like Students or Instructor. I have created 3 roles already in my Seed method, Admin, Student, and Instructor.

How can I write the Register method in the AccountController to connect the Student table and the ApplicationUser table?

Or like when a Student logs in, he is only able to see his account, not the list of students in the Students page?

I created a Foreign Key with the ApplicationUser table and the Person table (Student and Instructor tables inherit from Person table).

public abstract class Person
    {
    public int ID {get; set;}
    [ForeignKey("ApplicationUser")]
    public string ApplicationUserId { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }

AccountController.cs

public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    //Assign Role to User
                    await this.UserManager.AddToRoleAsync(user.Id, model.UserRoles);
                    return RedirectToAction("Index", "Home");
                }
                ViewBag.Name = new SelectList(context.Roles.Where(u => !u.Name.Contains("Admin"))
                    .ToList(), "Name", "Name");

                AddErrors(result);
            }

      // Register Student

RegisterViewModel

public class RegisterViewModel
    {
        [Required]
        [Display(Name = "UserRoles")]
        public string UserRoles { get; set; }

        [Required]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string Email { get; set; }

        //[Required]
        [Display(Name = "Username")]
        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; }
    }

Currently, a student user can see everyone, but I want the student to see only his account information. Note everyone's info.

Students Page

Configuration.cs (Seed method)

protected override void Seed(SchoolContext context)
        {
            var students = new List<Student>
            {
                new Student { FirstMidName = "Carson",   LastName = "Alexander",
                    EnrollmentDate = DateTime.Parse("2010-09-01") },
                new Student { FirstMidName = "Meredith", LastName = "Alonso",
                    EnrollmentDate = DateTime.Parse("2012-09-01") },
                new Student { FirstMidName = "Arturo",   LastName = "Anand",
                    EnrollmentDate = DateTime.Parse("2013-09-01") },
                new Student { FirstMidName = "Gytis",    LastName = "Barzdukas",
                    EnrollmentDate = DateTime.Parse("2012-09-01") },
                new Student { FirstMidName = "Yan",      LastName = "Li",
                    EnrollmentDate = DateTime.Parse("2012-09-01") },
                new Student { FirstMidName = "Peggy",    LastName = "Justice",
                    EnrollmentDate = DateTime.Parse("2011-09-01") },
                new Student { FirstMidName = "Laura",    LastName = "Norman",
                    EnrollmentDate = DateTime.Parse("2013-09-01") },
                new Student { FirstMidName = "Nino",     LastName = "Olivetto",
                    EnrollmentDate = DateTime.Parse("2005-09-01") }
            };


            students.ForEach(s => context.Students.AddOrUpdate(p => p.LastName, s));
            context.SaveChanges();

            //Adding users
            //if (!context.Users.Any(u => u.UserName == "Car"))
            //{
            //    var store = new UserStore<ApplicationUser>(context);
            //    var manager = new UserManager<ApplicationUser>(store);
            //    var user = new ApplicationUser { UserName = "sampleTwo@email.com", Email = "sampleTwo@email.com" };

            //    manager.Create(user, "password123");
            //}

I have googled a lot, but was not able to come up with a customized register method to work with different tables. Any assistance will be much helpful.

1
0
2/23/2017 3:46:28 PM

Popular Answer

You can alter your SQL hit based on user role.

      var whereClause = "";
      if (User.Roll = "Student")
      {
            whereClause = "WHERE USER_NAME = '" + User.Name + "'";
      }
            sqlQuery = "SELECT .... " + whereClause;

If the user is not an Admin, only their record will be returned.

2
2/23/2017 3:49:54 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow