コード例 #1
0
        public async Task <ActionResult> Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                bool result = _activeDirectoryService.ValidateCredentials(model.Domain, model.UserName, model.Password);
                if (result)
                {
                    var user = await _userService.GetUserByUserNameAsync(model.UserName);

                    if (user != null && user.Active)
                    {
                        var roleNames = user.Roles.Select(r => r.Name).ToList();
                        _authenticationService.SignIn(user, roleNames);

                        // Update LastLoginDate for future reference
                        user.LastLoginDate = _dateTime.Now;
                        await _userService.UpdateUserAsync(user);

                        _log.Info($"Login Successful: {user.UserName}");

                        // Redirect to return URL
                        if (!string.IsNullOrEmpty(returnUrl) && !string.Equals(returnUrl, "/") && Url.IsLocalUrl(returnUrl))
                        {
                            return(RedirectToLocal(returnUrl));
                        }

                        // User is in a role, so redirect to Administration area
                        if (roleNames.Contains(Constants.RoleNames.Developer) ||
                            roleNames.Contains(Constants.RoleNames.ApplicationManager))
                        {
                            return(RedirectToRoute("Dashboard"));
                        }

                        return(RedirectToAction("Index", "Home"));
                    }
                    _log.Info($"Authorization Fail: {model.UserName}");
                    ModelState.AddModelError("", Constants.Messages.NotAuthorized);
                }
                else
                {
                    _log.Info($"Login Fail: {model.UserName}");
                    ModelState.AddModelError("", "Incorrect username or password.");
                }
            }
            model.AvailableDomains = await GetDomains();

            return(View("Login", model));
        }