public ActionResult CreateUser()
        {
            string   controllername   = "";
            string   actionname       = "";;
            AppUsers usermodel        = new AppUsers();
            string   connectionString = ConfigurationManager.ConnectionStrings["NHSConStr"].ConnectionString;
            DBEngine dBEngine         = new DBEngine(connectionString);
            int      dbReturn         = dBEngine.CreateUser(Request.Form["EmailID"], Request.Form["FirstName"], Request.Form["LastName"], Request.Form["UserName"], Request.Form["ddlSpeciality"], Convert.ToInt32(Request.Form["DischargeRole"]), Request.Form["Code"], 0);

            if (dbReturn == 0)
            {
                Alert alertMessage = new Alert();
                alertMessage.AlertType   = ALERTTYPE.Error;
                alertMessage.MessageType = ALERTMESSAGETYPE.TextWithClose;
                alertMessage.Message     = "We have taken your request for registering to the CORS platform. You would get a confirmation email once your registration is approved.";
                TempData["AlertMessage"] = alertMessage.Message;
                List <NotificationSettings> settings = dBEngine.GetNotificationSettingsByTrigger("Create User", 0);
                if (settings.Count > 0)
                {
                    for (int counter = 0; counter < settings.Count; counter++)
                    {
                        List <AppUsers> users = dBEngine.GetUsersByRoleID(settings[counter].RoleID, 0);
                        for (int count = 0; count < users.Count; count++)
                        {
                            SendEmail(users[count].EmailID, "CORS - New User for Approval", settings[counter].EmailTemplate.Replace("##FullName##", Request.Form["FirstName"] + " " + Request.Form["LastName"]));
                        }
                    }
                }
                controllername = "Account";
                actionname     = "Index";
            }
            else if (dbReturn == -1)
            {
                Alert alertMessage = new Alert();
                alertMessage.AlertType   = ALERTTYPE.Error;
                alertMessage.MessageType = ALERTMESSAGETYPE.TextWithClose;
                alertMessage.Message     = "User already exists, please proceed to login or contact administrator.";
                TempData["AlertMessage"] = alertMessage.Message;
                controllername           = "Account";
                actionname = "Index";
            }
            else
            {
                usermodel = dBEngine.ValidateUser(Request.Form["UserName"], "");
                Session["LoginUserID"]         = usermodel.ID;
                Session["UserName"]            = Request.Form["UserName"];
                Session["FirstName"]           = usermodel.FirstName;
                Session["LastName"]            = usermodel.LastName;
                Session["StartDate"]           = "";
                Session["EndDate"]             = "";
                Session["WardDeath"]           = "";
                Session["PatientType"]         = "";
                Session["DischargeConsultant"] = "";
                Session["Speciality"]          = "";
                Session["TotalDeaths"]         = 0;
                Session["QAPCount"]            = 0;
                Session["MedCount"]            = 0;
                Session["Role"] = usermodel.Role;
                controllername  = "Home";
                actionname      = "Index";
            }
            return(RedirectToAction(actionname, controllername));
        }
        public ActionResult Login()
        {
            string controllername = "";

            ViewBag.AlertMessage = "";
            string   actionname       = "";
            string   username         = Request.Form["Email"];
            string   password         = Request.Form["Password"];
            string   domain           = "";
            string   connectionString = ConfigurationManager.ConnectionStrings["NHSConStr"].ConnectionString;
            DBEngine dBEngine         = new DBEngine(connectionString);
            bool     isValidFromAD    = false;

            if (username.IndexOf("\\") > 0)
            {
                username = username.Split("\\".ToCharArray())[1];
                // domain = username.Split("\\".ToCharArray())[0];
            }
            domain = dBEngine.GetDomainName(0);

            AppUsers usermodel = new AppUsers();

            try
            {
                //isValidFromAD = ValidateCredentials(username, password, domain);
                isValidFromAD = true;

                if (isValidFromAD)
                {
                    usermodel  = dBEngine.ValidateUser(username, password);
                    actionname = "Index";
                    if (usermodel.IsFound)
                    {
                        //Session.Abandon();
                        Session.Timeout                = 1440;
                        Session["LoginUserID"]         = usermodel.ID;
                        Session["UserName"]            = username;
                        Session["FirstName"]           = usermodel.FirstName;
                        Session["LastName"]            = usermodel.LastName;
                        Session["StartDate"]           = "";
                        Session["EndDate"]             = "";
                        Session["WardDeath"]           = "";
                        Session["PatientType"]         = "";
                        Session["DischargeConsultant"] = "";
                        Session["Speciality"]          = "";
                        Session["TotalDeaths"]         = 0;
                        Session["QAPCount"]            = 0;
                        Session["MedCount"]            = 0;
                        Session["Role"]                = usermodel.Role;
                        int dbReturn = dBEngine.UpdateLoginDateTime(usermodel.ID);
                        controllername = "Home";
                    }
                    else
                    {
                        Alert alertMessage = new Alert();
                        alertMessage.AlertType   = ALERTTYPE.Error;
                        alertMessage.MessageType = ALERTMESSAGETYPE.TextWithClose;
                        alertMessage.Message     = "You are not authorised to access this app. Please call 8066/6761/5252/8335.";
                        TempData["AlertMessage"] = alertMessage.Message;
                        controllername           = "Account";
                    }
                }
                else
                {
                    Alert alertMessage = new Alert();
                    alertMessage.AlertType   = ALERTTYPE.Error;
                    alertMessage.MessageType = ALERTMESSAGETYPE.TextWithClose;
                    alertMessage.Message     = "Credentials provided do not match with AD.";
                    TempData["AlertMessage"] = alertMessage.Message;
                    controllername           = "Account";
                }
            }
            catch (Exception ex)
            {
                Alert alertMessage = new Alert();
                alertMessage.AlertType   = ALERTTYPE.Error;
                alertMessage.MessageType = ALERTMESSAGETYPE.TextWithClose;
                alertMessage.Message     = "SQL/AD Connection Error. Error Details - " + ex.Message;
                TempData["AlertMessage"] = alertMessage.Message;
                controllername           = "Account";
            }
            return(RedirectToAction(actionname, controllername));// RedirectToAction("Index");
        }