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

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

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemple #2
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new DiaryUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

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

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
        public async Task <ActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                DiaryUser user = new DiaryUser {
                    UserName = model.Email, Email = model.Email
                };
                IdentityResult result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Login", "Account"));
                }
                else
                {
                    foreach (string error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
            }
            return(View(model));
        }
        protected override void Seed(ApplicationDbContext context)
        {
            //Roles
            string[] roles =
            {
                "admin", "teacher", "parent", "children"
            };
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));

            foreach (var r in roles)
            {
                roleManager.Create(new IdentityRole(r));
            }
            //Users
            ApplicationUserManager manager = new ApplicationUserManager(new UserStore <DiaryUser>(context));
            string password = "******";
            var    user     = new DiaryUser()
            {
                Email    = "*****@*****.**",
                UserName = "******"
            };

            manager.Create(user, password);

            manager.AddToRole(user.Id, "children");
            var teacher = new DiaryUser()
            {
                Email    = "*****@*****.**",
                UserName = "******"
            };

            manager.Create(teacher, password);
            manager.AddToRole(teacher.Id, "teacher");
            var parent = new DiaryUser()
            {
                Email    = "*****@*****.**",
                UserName = "******"
            };

            manager.Create(parent, password);
            manager.AddToRole(parent.Id, "parent");

            var admin = new DiaryUser()
            {
                Email    = "*****@*****.**",
                UserName = "******"
            };

            manager.Create(admin, password);
            manager.AddToRole(admin.Id, "admin");

            // Add Lessons
            var lesson = new Lesson {
                Title = "Lesson#1", TeacherId = teacher.Id
            };
            var lesson1 = new Lesson {
                Title = "Lesson#2", TeacherId = teacher.Id
            };
            var lesson2 = new Lesson {
                Title = "Lesson#3", TeacherId = teacher.Id
            };

            context.Lessons.Add(lesson);
            context.Lessons.Add(lesson1);
            context.Lessons.Add(lesson2);
            //Add schooles
            var sch = new SchoolClass {
                Title = "Class#1"
            };
            var sch1 = new SchoolClass {
                Title = "Class#2"
            };

            context.SchoolClasses.Add(sch);
            context.SchoolClasses.Add(sch1);
            //Children Data (withoud SchoolClass)
            context.ChildrenData.Add(new Models.People.ChildrenData()
            {
                ChildrenId = user.Id, ParentId = parent.Id, SchoolClassId = 1
            });
            //Schelude lesonse
            //context.ScheduleLessons.Add(new ScheduleLesson() { LessonId = lesson.Id, SchoolClassId = sch.Id, Order = 2, DayNumber = 2 });
            //context.ScheduleLessons.Add(new ScheduleLesson() { LessonId = lesson1.Id, SchoolClassId = sch.Id, Order = 1, DayNumber = 1 });

            context.SaveChanges();
        }