Пример #1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.UserName, Email = model.Email, DateCreated = DateTime.Now
                };
                var result = await UserManager.CreateAsync(user, model.Password);

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

                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    IDictionary <string, object> dicVal = new Dictionary <string, object>();
                    dicVal.Add("userId", user.Id);
                    dicVal.Add("code", code);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new RouteValueDictionary(dicVal), "http", urlAddress, true);

                    await UserManager.SendEmailAsync(
                        user.Id,
                        "Confirm your account",
                        "<p>Welcome to the <span style=\"font-weight:bold; color:#000080\"> MACA</span> portal!</p> " +
                        "<br />" +
                        "<p>Please, confirm your account by clicking <a href=\"" + callbackUrl + "\">THIS.</a></p>"
                        );

                    // Uncomment to debug locally
                    //ViewBag.Link = callbackUrl;

                    //ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
                    //   + "before you can log in.";
                    ViewBag.Message = "Check your email and confirm the address. Only confirmed users can log in.";

                    Auxiliaries.AddEvent(dbEvents, dbEventTypes, "REG", Request.UserHostAddress, Request.UserHostName, User.Identity);

                    return(View("Info"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Пример #2
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // Require the user to have a confirmed email before they can log on.
            var user = await UserManager.FindByEmailAsync(model.Email);

            if (user != null)
            {
                if (!await UserManager.IsEmailConfirmedAsync(user.Id))
                {
                    //ViewBag.errorMessage = "You must have a confirmed email to log on.";
                    ViewBag.errorMessage = "Za prijavo morate potrditi svoj e-poštni naslov.";
                    return(View("Error"));
                }
            }

            // 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(user.UserName, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                Auxiliaries.AddEvent(dbEvents, dbEventTypes, "LOG_IN", Request.UserHostAddress, Request.UserHostName, User.Identity);
                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("", "Prijava je bila neuspešna.");
                return(View(model));
            }
        }