Beispiel #1
0
        public async Task<ActionResult> Create(CreateModel model)
        {
            if (ModelState.IsValid)
            {
                AppUser user = new AppUser {FirstName = model.FirstName, LastName = model.LastName, UserName = model.UserName, Email = model.Email };
                IdentityResult result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            return View(model);
        }
        public async Task<ActionResult> Create(CreateModel model)
        {
            if (ModelState.IsValid)
            {
                AppUser user = new AppUser
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    UserName = model.UserName,
                    Email = model.Email,
                    Address = model.Address,
                    Zip = model.Zip,
                    City = model.City.ToString(),
                    Country = "Danmark",
                    Avatar = model.Avatar,
                    Gender = model.Gender.ToString(),
                    Biography = model.Biography,
                    Birthday = model.Birthday,
                    PrimaryLanguage = model.PrimaryLanguage.ToString(),
                    CreatedAt = DateTime.Now
                };
                IdentityResult result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {


                    var addedToRole = await UserManager.AddToRoleAsync(user.Id, model.Role);
                    return RedirectToAction("Index");
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            ViewBag.Role = new SelectList(db.Roles, "Name", "Name");

            return View(model);
        }
        public async Task<ActionResult> RegisterStudent(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                AppUser user = new AppUser
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    UserName = model.Email,
                    Email = model.Email,
                    Country = "Danmark",
                    CreatedAt = DateTime.Now
                };
                IdentityResult result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var currentUser = UserManager.FindByName(user.UserName);

                    var roleresult = UserManager.AddToRole(currentUser.Id, "Student");

                    return RedirectToAction("Index", "Home", new { area = "Students"});
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            return View(model);
        }