Example #1
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                ApplicationUser identity_user = UserManager.FindByEmail(model.Email);
                App_Database    app_database  = new App_Database(); // App_database object to use the application database for adding a user
                User            user          = app_database.User.Find(identity_user.Id);
                user.LastLoginTime = DateTime.Now;
                app_database.SaveChanges();
                app_database.Dispose();
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }
Example #2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    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);

                    App_Database app_database = new App_Database(); // App_database object to use the application database for adding a user

                    UserManager <ApplicationUser> userManager;
                    userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext()));

                    var found_user = userManager.FindByEmail(model.Email); // Find user in Identity database by email

                    // Create user record for application database
                    if (model.GroupID != 0) // If group selected, create user with selected group
                    {
                        var application_user = new User {
                            UserID = found_user.Id, UserInterface = "Light", UserCreationDate = DateTime.Today, GroupID = model.GroupID
                        };
                        app_database.User.Add(application_user);
                    }
                    else // Create user without group assigned
                    {
                        var application_user = new User {
                            UserID = found_user.Id, UserInterface = "Light", UserCreationDate = DateTime.Today
                        };
                        app_database.User.Add(application_user);
                    }
                    if (string.IsNullOrEmpty(model.isInstructor) == false)
                    {
                        userManager.AddToRole(found_user.Id, "Instructor");
                    }
                    else
                    {
                        userManager.AddToRole(found_user.Id, "Learner");
                    }
                    app_database.SaveChanges(); // COMMIT changes to database
                    app_database.Dispose();

                    // 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("ShowUsers", "Account"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }