Example #1
0
        public async Task <ActionResult> Login(LoginVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var user = accountManager.GetUser(model.Email);
                    if (user != null)
                    {
                        if (user.Password == model.Password)
                        {
                            await accountManager.SaveLastLoginAsync(user.Id);

                            int userDefaultTeamId = user.DefaultTeamId ?? 0;

                            SetUserIDToSession(user.Id, userDefaultTeamId, user.Name);

                            return(RedirectToAction("index", "dashboard"));
                        }
                    }
                }
                ModelState.AddModelError("", "Username/Password is incorrect!");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Oops! Something went wrong :(");
                log.Error(ex);
            }
            return(View(model));
        }
        public async Task <ActionResult> Login(LoginVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var user = accountManager.GetUser(model.Email);
                    if (user != null)
                    {
                        var appUser = new AppUser {
                            UserName = user.EmailAddress, Id = user.Id.ToString()
                        };
                        //var user1 = await um.FindAsync(model.Email, model.Password);
                        //if (user1!= null)
                        //{
                        await SignInAsync(appUser, model.RememberMe);

                        //   // return RedirectToLocal(returnUrl);
                        //}


                        if (user.Password == model.Password)
                        {
                            await accountManager.SaveLastLoginAsync(user.Id);

                            int userDefaultTeamId = user.DefaultTeamId ?? 0;



                            var claims = new[] {
                                new Claim(ClaimTypes.Name, user.Name),
                                new Claim(ClaimTypes.Email, user.EmailAddress)
                            };
                            //var identity = new ClaimsIdentity(claims,DefaultAuthenticationTypes.ApplicationCookie);
                            //ClaimsPrincipal principal = new ClaimsPrincipal(identity);
                            //Thread.CurrentPrincipal = principal;
                            //var context = Request.GetOwinContext();
                            //var authManager = context.Authentication;

                            //authManager.SignIn(new AuthenticationProperties { IsPersistent = true }, identity);

                            //AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                            //var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
                            //AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);



                            SetUserIDToSession(user.Id, userDefaultTeamId, user.Name);

                            return(RedirectToAction("index", "dashboard"));
                        }
                    }
                }
                ModelState.AddModelError("", "Username/Password is incorrect!");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Oops! Something went wrong :(");
                log.Error(ex);
            }
            return(View(model));
        }