コード例 #1
0
        public ActionResult ChangeCurrentTeam(ChangeCurrentTeamViewModel model, string returnUrl)
        {
            if (model == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = db.Users.Find(User.Identity.GetUserId());
            var team = user.Teams.SingleOrDefault(x => x.Id == model.TeamId);

            if (team == null)
            {
                return(View(model));
            }

            MembershipTeam.SetCurrentTeam(team.Name);
            if (string.IsNullOrEmpty(returnUrl))
            {
                return(RedirectToAction("Index", "Home"));
            }
            return(Redirect(returnUrl));
        }
コード例 #2
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:
                var user = db.Users.Single(x => x.Email == model.Email);
                var team = user.Teams.Single(x => x.Type == TeamType.Fictive);
                MembershipTeam.SetCurrentTeam(team.Name);
                return(RedirectToUrl(returnUrl));

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

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

            case SignInStatus.Failure:
            default:
                ViewBag.ReturnUrl = returnUrl;
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }
コード例 #3
0
        public async Task <ActionResult> VerifyCode(VerifyCodeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // The following code protects for brute force attacks against the two factor codes.
            // If a user enters incorrect codes for a specified amount of time then the user account
            // will be locked out for a specified amount of time.
            // You can configure the account lockout settings in IdentityConfig
            var result =
                await
                SignInManager.TwoFactorSignInAsync(model.Provider, model.Code, isPersistent : model.RememberMe,
                                                   rememberBrowser : model.RememberBrowser);

            switch (result)
            {
            case SignInStatus.Success:
                var userName = User.Identity.GetUserName();
                var user     = db.Users.Single(x => x.UserName == userName);
                var team     = user.Teams.Single(x => x.Type == TeamType.Fictive);
                MembershipTeam.SetCurrentTeam(team.Name);
                return(RedirectToUrl(model.ReturnUrl));

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

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid code.");
                return(View(model));
            }
        }
コード例 #4
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User {
                    UserName = model.Email, Email = model.Email
                };
                var fictiveTeam = new Team()
                {
                    Name       = user.UserName,
                    Type       = TeamType.Fictive,
                    Performers = new[] { user }
                };
                user.Teams.Add(fictiveTeam);

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    MembershipTeam.SetCurrentTeam(fictiveTeam.Name);
                    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));
        }