public async Task<ActionResult> Login(LoginViewModel model, string returnUrl, int portalId)
        {
            try
            {
                ReadDataFromRouteData("login");
                model.Resources = resources;
                model.portalId = portalId;
                model.clientId = clientId;

                if (ModelState.IsValid)
                {
                    // Form data will be validated against the information stored in Database
                    //and user object will be returned if its authenticated.

                    PortalAPI.Models.User user = null;
                    var ADSIEnabled = false;
                    PortalAPI.Repositories.Concrete.DataConfigurationRepository dataConfigurationRepository = new PortalAPI.Repositories.Concrete.DataConfigurationRepository(System.Web.HttpContext.Current.Application["KBDataPath"].ToString(), System.Web.HttpContext.Current.Application["KBInstallPath"].ToString());

                    if (dataConfigurationRepository != null)
                    { ADSIEnabled = dataConfigurationRepository.ADEnabled; }

                    if (ADSIEnabled)
                    {
                        var isvalidADUser = _activeDirectoryManager.Authenticate(model.UserName, model.Password);
                        if (isvalidADUser)
                        {
                            user = _usersManager.ADLogin(model.UserName);
                        }
                        else
                        {
                            user = _usersManager.Login(model.UserName, model.Password);
                        }
                    }
                    else
                    {
                        user = _usersManager.Login(model.UserName, model.Password);
                    }
                    if (user != null)
                    {
                        SessionExtensions.LogPortalVisit(user.UserId, portalId, clientId, 2, "");

                        var appUser = new ApplicationUser() { UserName = user.LoginName };
                        await SignInAsync(appUser, model.RememberMe);
                        Session.Add("UserSession_" + portalId.ToString(), user);
                        var routrDict = new RouteValueDictionary();
                        routrDict.Add("Controller", "Home");
                        routrDict.Add("Action", "Index");
                        routrDict.Add("clientId", clientId);
                        routrDict.Add("portalId", portalId);
                        return RedirectToLocal(returnUrl, routrDict);
                    }
                    else
                    {
                        //ModelState.AddModelError("", "Invalid username or password.");
                        ViewBag.Message = "1";
                        ViewBag.clientId = clientId;
                        ViewBag.portalId = portalId;
                        return RedirectToAction("Login/" + model.clientId + "/" + portalId, "Account", new { Message = "1" });
                    }

                }

                return RedirectToAction("Login/" + model.clientId + "/" + portalId, "Account");
                //return View(model);
            }
            catch (Exception ex)
            {
                KBCustomException kbCustExp = KBCustomException.ProcessException(ex, KBOp.LoginAction, KBErrorHandler.GetMethodName(), GeneralResources.LoginError,
                    new KBExceptionData("returnUrl", returnUrl), new KBExceptionData("portalId", portalId), new KBExceptionData("clientId", clientId));
                throw kbCustExp;
            }
        }
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //if (filterContext.RouteData.GetRequiredString("action").ToLowerInvariant() != "index")
            //{
            //    return;
            //}

            //string path = GetConfigXmlPath(filterContext.RouteData);
            //PortalConfigurationBuilder builder = filterContext.HttpContext.Session.GetPortalConfigurationBuilder();
            //builder.LoadPortalConfigurationManager(_clientId, _portalId, path);

            //1. No portal Sessions
            //2. Different Portal Sessions
            //3. dictionary is empty
            int portalId = GetPortalId(filterContext.RouteData);
            int clientId = GetClientId(filterContext.RouteData);
            var portalSessions = filterContext.HttpContext.Session.GetPortalSessions();
            var portalResourceSessions = filterContext.HttpContext.Session.GetPortalResourceSessions();
            if (!portalSessions.HasPortalSession(portalId))
            {
                Portal portal = null;
                try
                {
                    portal = filterContext.HttpContext.Application.LoadPortalConfiguration(clientId, portalId, true);

                    if (portal == null)
                    {
                        KBCustomException kbCustUserExp = KBCustomException.ProcessException(null, KBOp.LoadConfigFile, KBErrorHandler.GetMethodName(), GeneralResources.IOError + ". " + GeneralResources.ConfigPortalAdminError, LogEnabled.False,
                            new KBExceptionData("clientId", clientId), new KBExceptionData("portalId", portalId));
                        kbCustUserExp.SetUrlRouteKeyValues(new KBUrlRoute("Controller", "Admin"), new KBUrlRoute("Action", "Index"), new KBUrlRoute("portalId", portalId));
                        throw kbCustUserExp;
                    }
                    string url = HttpContext.Current.Request.Url.ToString();

                    SessionExtensions.LogPortalVisit(0, portalId, clientId, 1, url);

                }
                catch (Exception ex)
                {
                    KBCustomException kbCustExp = KBCustomException.ProcessException(ex, KBOp.LoadConfigFile, KBErrorHandler.GetMethodName(), GeneralResources.GeneralError,
                        new KBExceptionData("clientId", clientId), new KBExceptionData("portalId", portalId));
                    if (ex.InnerException is FileNotFoundException)
                    {
                        // Build new exception with user friendly error message
                        KBCustomException kbCustUserExp = KBCustomException.ProcessException(ex.InnerException, KBOp.LoadConfigFile, KBErrorHandler.GetMethodName(), GeneralResources.IOError + ". " + GeneralResources.ConfigPortalAdminError,
                            new KBExceptionData("clientId", clientId), new KBExceptionData("portalId", portalId));
                        throw kbCustUserExp;
                    }
                    else
                    {
                        throw kbCustExp;
                    }
                }

                PortalSession portalSession = new PortalSession() { Portal = portal };
                CacheDependency cd = new CacheDependency(filterContext.HttpContext.Application["KBDataPath"] + @"/knowledgebase/PortalConfiguration/" + clientId + "/" + portalId + "/configuration/portal-config.xml");
                filterContext.HttpContext.Cache.Insert(portalId.ToString(), "portal", cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
                portalSessions.AddPortalSession(portalSession);
            }
            // update portal session based on cache depedency
            if (filterContext.HttpContext.Cache[portalId.ToString()] == null)
            {
                Portal portal = filterContext.HttpContext.Application.LoadPortalConfiguration(clientId, portalId, true);
                PortalSession portalSession = portalSessions.GetPortalSession(portalId, clientId);
                portalSession.Portal = portal;
                CacheDependency cd = new CacheDependency(filterContext.HttpContext.Application["KBDataPath"] + @"/knowledgebase/PortalConfiguration/" + clientId + "/" + portalId + "/configuration/portal-config.xml");
                filterContext.HttpContext.Cache.Insert(portalId.ToString(), "portal", cd);
            }

            Portal lportal = portalSessions.GetPortalSession(portalId, clientId).Portal;

            //check iprestriction
            if (!CheckPortalIpWithinIpRestrictions(filterContext, lportal))
            {
                // Client ip is restricted, redirect to error page.
                KBCustomException kbCustExp = KBCustomException.ProcessException(null, KBOp.CheckSourceIp, KBErrorHandler.GetMethodName(), GeneralResources.IPAddressBlockedError,
                    new KBExceptionData("clientId", clientId), new KBExceptionData("portalId", portalId));
                throw kbCustExp;
            }
            filterContext.HttpContext.Session["PortalType"] = lportal.PortalType;

            if (!portalResourceSessions.HasResourceSession(portalId))
            {
                var resource = filterContext.HttpContext.Application.LoadPortalResource(clientId, portalId, lportal.Language.Name, true);
                CacheDependency cdlang = new CacheDependency(filterContext.HttpContext.Application["KBDataPath"] + @"/knowledgebase/PortalConfiguration/" + clientId + "/" + portalId + "/languages/" + Utilities.GetResourceFileName(lportal.Language.Name));
                filterContext.HttpContext.Cache.Insert(portalId.ToString() + lportal.Language.Name.ToString(), "portallanguage", cdlang, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
                portalResourceSessions.AddPortalResourceSession(resource);
            }

            // update portalresource session based on cache depedency
            if (filterContext.HttpContext.Cache[portalId.ToString() + lportal.Language.Name.ToString()] == null)
            {
                portalResourceSessions.RemovePortalResourceSession(portalId);
                var resource = filterContext.HttpContext.Application.LoadPortalResource(clientId, portalId, lportal.Language.Name, true);
                portalResourceSessions.AddPortalResourceSession(resource);
                CacheDependency cdlang = new CacheDependency(filterContext.HttpContext.Application["KBDataPath"] + @"/knowledgebase/PortalConfiguration/" + clientId + "/" + portalId + "/languages/" + Utilities.GetResourceFileName(lportal.Language.Name));
                filterContext.HttpContext.Cache.Insert(portalId.ToString() + lportal.Language.Name.ToString(), "portallanguage", cdlang, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
            }

            if (lportal.PortalType != PortalType.Open)
            {
                var isAuthenticated = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
                if (!isAuthenticated)
                {
                    filterContext.HttpContext.Session.RemoveUser(portalId);
                }
                var user = filterContext.HttpContext.Session.GetUser(portalId);
                //Get AD enabled from shared configuration file.
                var ADSIEnabled = false;
                PortalAPI.Repositories.Concrete.DataConfigurationRepository dataConfigurationRepository = new PortalAPI.Repositories.Concrete.DataConfigurationRepository(HttpContext.Current.Application["KBDataPath"].ToString(), HttpContext.Current.Application["KBInstallPath"].ToString());

                if (dataConfigurationRepository != null)
                { ADSIEnabled = dataConfigurationRepository.ADEnabled; }

                if (user == null)
                {
                    if ((ADSIEnabled) && (HttpContext.Current.Request.LogonUserIdentity.IsAuthenticated))
                    {
                        string uname = HttpContext.Current.User.Identity.Name.ToLower();
                        if (uname == string.Empty) uname = Environment.UserName.ToLower();
                        logger.Info("AD user name:" + uname);
                        if (uname.Contains(@"\"))
                        {
                            string[] unameArray = uname.Split('\\');
                            if (unameArray.Length == 2)
                            {
                                string username = unameArray[1];
                                // redirect to AD login
                                var routeDict = new RouteValueDictionary();
                                routeDict.Add("Controller", "Account");
                                routeDict.Add("Action", "ADLogin");
                                routeDict.Add("portalId", portalId);
                                routeDict.Add("username", username);
                                filterContext.Result = new RedirectToRouteResult(routeDict);
                            }
                        }
                    }
                    else
                    {
                        // redirect to login page
                        var routeDict = new RouteValueDictionary();
                        routeDict.Add("Controller", "Account");
                        routeDict.Add("Action", "Login");
                        routeDict.Add("portalId", portalId);
                        filterContext.Result = new RedirectToRouteResult(routeDict);
                    }
                }
                else
                {
                    if (!user.Profile.AuthorizedPortals.Contains(lportal.PortalId))
                    {
                        KBCustomException kbCustUserExp = KBCustomException.ProcessException(null, KBOp.GetUserAuth, KBErrorHandler.GetMethodName(), GeneralResources.UserNotAuthedError, LogEnabled.False,
                            new KBExceptionData("clientId", clientId), new KBExceptionData("portalId", portalId));
                        kbCustUserExp.SetUrlRouteKeyValues(new KBUrlRoute("Controller", "Account"), new KBUrlRoute("Action", "LogOffNotAuthorized"));
                        logger.ErrorException(kbCustUserExp.DataSummary(), kbCustUserExp);
                        kbCustUserExp.LoggerCalled = true;  // unusual scenario of needing to call logger after adding more data to exception object
                        throw kbCustUserExp;
                    }
                }
            }
        }