Example #1
0
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    using (DirectoryEntry de = new DirectoryEntry(ConfigurationManager.ConnectionStrings[ConfigurationKey.ActiveDirectoryConnectionString].ToString(), model.UserName, model.Password))
                    {
                        using (DirectorySearcher adSearch = new DirectorySearcher(de))
                        {
                            const string _filterFormat = "(sAMAccountName={0})";
                            adSearch.Filter = string.Format(_filterFormat, model.UserName);
                            SearchResult adSearchResult = adSearch.FindOne();

                            if (adSearchResult.Properties.Count != 0)
                            {
                                const string _employeeIdkey = "employeeid";
                                const string _emailkey      = "userprincipalname";

                                model.EmployeeId = adSearchResult.Properties[_employeeIdkey][0].ToString();
                                model.Email      = adSearchResult.Properties[_emailkey][0].ToString();
                                Session[SessioKey.LoginCredential] = model;

                                if (model.Email == null || string.IsNullOrEmpty(model.Email))
                                {
                                    ModelState.AddModelError(string.Empty, Resources.Pms.Login_InvalidLogin);
                                }
                                else
                                {
                                    IList <CodeMessage> messages = new List <CodeMessage>();
                                    this._userManagementRepository.ServiceHeaders = PresentationUtility.GetBasicHeaders(new string[] { ServiceHeaderKey.EmployeeId, ServiceHeaderKey.UserName, ServiceHeaderKey.Email });
                                    IEmployee employee = _userManagementRepository.EmployeeGet(SearchKey.Email, model.Email, out messages);
                                    if (employee == null)
                                    {
                                        Session[SessioKey.LoginCredential] = null;

                                        foreach (CodeMessage message in messages)
                                        {
                                            ModelState.AddModelError(string.Empty, message.Name);
                                        }

                                        return(View(model));
                                    }
                                    else
                                    {
                                        model.PersonId     = employee.PersonId;
                                        model.EmployeeId   = employee.EmployeeId;
                                        model.AccountState = string.IsNullOrEmpty(employee.PersonObject.PersonStatus) ? 1 : Int16.Parse(employee.PersonObject.PersonStatus);

                                        Session[SessioKey.LoginCredential] = model;
                                        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                                    }
                                }
                            }
                        }
                    }

                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith(@"\//"))
                    {
                        return(Redirect(returnUrl));
                    }
                    else
                    {
                        return(RedirectToAction(ControllerActionString.Index, ControllerString.Home));
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, Resources.Pms.Login_InvalidLogin);
                }
            }

            return(View(model));
        }