Exemplo n.º 1
0
        public async Task <IActionResult> Login(UserModel userModel)
        {
            BusFacCore busFacCore = new BusFacCore(_settings);
            BusUser    busUser    = new BusUser();

            if ((!string.IsNullOrEmpty(userModel.Username)) && (!string.IsNullOrEmpty(userModel.Password)) &&
                (busUser.IsValidUsername(userModel.Username)) && (busUser.IsValidPasswd(userModel.Password)))
            {
                bool UserExist = busFacCore.Exist(userModel.Username);
                if (UserExist)
                {
                    User user = busFacCore.UserAuthenticate(userModel.Username, userModel.Password);
                    if ((user != null) && (user.UserID > 0))
                    {
                        var claims = new[] {
                            new Claim("name", userModel.Username)
                        };

                        var principal = new ClaimsPrincipal(
                            new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme));

                        await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", principal);

                        return(RedirectToAction("Index", "Dashboard"));
                    }
                }
            }

            return(RedirectToAction("Index", "Home"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Login(UserModel userModel)
        {
            BusFacCore busFacCore = new BusFacCore(_settings);
            BusUser    busUser    = new BusUser();

            if ((!string.IsNullOrEmpty(userModel.Username)) && (!string.IsNullOrEmpty(userModel.Password)) &&
                (busUser.IsValidUsername(userModel.Username)) && (busUser.IsValidPasswd(userModel.Password)))
            {
                bool UserExist = busFacCore.Exist(userModel.Username);
                if (UserExist)
                {
                    User user = busFacCore.UserAuthenticate(userModel.Username, userModel.Password);
                    if ((user != null) && (user.UserID > 0))
                    {
                        userModel.DisplayName = userModel.Username;
                        if ((!string.IsNullOrEmpty(userModel.FirstName)) && (!string.IsNullOrEmpty(userModel.LastName)))
                        {
                            userModel.DisplayName = userModel.LastName + ", " + userModel.FirstName;
                        }

                        var claims = new[] {
                            new Claim("name", userModel.Username),
                            new Claim(ClaimTypes.Name, userModel.DisplayName, ClaimValueTypes.String)
                        };

                        var principal = new ClaimsPrincipal(
                            new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme));

                        await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", principal);

                        return(RedirectToAction("Index", "Dashboard"));
                    }
                }
            }

            ViewData["InvalidCredentials"] = true;
            return(View("Index"));
        }