예제 #1
0
        /// <summary>
        /// Display screen to add user in system
        /// </summary>
        /// <returns>User View</returns>
        public ActionResult NewUser()
        {
            var user = new DeluxeOM.Models.Account.User()
            {
                Active         = true,
                Roles          = _service.GetRoles(),
                SelectedRoleId = 4
            };

            return(View(user));
        }
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            User userModel = new DeluxeOM.Models.Account.User()
            {
                Email    = model.Email,
                Password = model.Password,
            };

            if (userModel.Equals(null) && ModelState.IsValid)
            {
                return(View(model));
            }


            var authResult = _service.Authenticate(userModel);

            User user = (User)authResult.DataObject;

            if (authResult.Status == AuthStatus.Success)
            {
                ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);

                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.FirstName));
                user.Privs.ForEach((role) => identity.AddClaim(new Claim(ClaimTypes.Role, role.PrivName)));
                identity.AddClaim(new Claim(ClaimTypes.Name, user.FirstName));
                identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));
                identity.AddClaim(new Claim(ClaimTypes.GivenName, user.FirstName));
                identity.AddClaim(new Claim(ClaimTypes.Surname, user.LastName));
                identity.AddClaim(new Claim(ClaimTypes.Sid, user.UserId.ToString()));
                if (!string.IsNullOrEmpty(user.PhoneNumber))
                {
                    identity.AddClaim(new Claim(ClaimTypes.MobilePhone, user.PhoneNumber));
                }
                //bool pippo = User.IsInRole("Admin");
                AuthenticationManager.SignIn(identity);
                //bool bippo = User.IsInRole("Admin");

                // if remember me clicked, save login name in cookie
                if (model.RememberMe)
                {
                    var userNameCookie = new HttpCookie("DeluxeOMUsername");
                    userNameCookie.Values["Username"] = model.Email;

                    userNameCookie.Expires = DateTime.Now.AddDays(14);
                    Response.Cookies.Add(userNameCookie);
                }
                else
                {
                    // remove cookie if is remember me cleared
                    if (Request.Cookies["DeluxeOMUsername"] != null)
                    {
                        var userNameCookie = new HttpCookie("DeluxeOMUsername");
                        userNameCookie.Expires = DateTime.Now.AddDays(-1d);
                        Response.Cookies.Add(userNameCookie);
                    }
                }

                // Login Successful
                if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
                {
                    return(Redirect(returnUrl));
                }
                else
                {
                    return(RedirectToAction("Orders", "Order"));
                }
            }
            else
            {
                // Login Failed
                if (authResult.Status == AuthStatus.PasswordExpiredAllowChange)
                {
                    ResetPasswordViewModel resetModel = new ResetPasswordViewModel()
                    {
                        Email = model.Email
                    };

                    //return RedirectToAction("ExpiredPassword", "Account", resetModel);
                    return(RedirectToAction("ExpiredPassword", "Account", new { email = resetModel.Email }));
                }
                //ViewBag.Message = authResult.Message;

                ModelState.AddModelError("", authResult.Message);
            }


            return(View(model));

            #region Original
            //// 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:
            //        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);
            //}
            #endregion
        }