Example #1
0
        public string login([FromForm] Models.FormModel user)
        {
            var output = new Dictionary <string, string>();

            try
            {
                //Define credentials
                var username = user.Username;
                var password = user.Password;
                //Try login service method
                var token = _ls.SignIn(username, password);
                //If successful
                var usernameCookieOptions = new CookieOptions {
                    SameSite = SameSiteMode.Strict
                };
                var authTokenCookieOptions = new CookieOptions {
                    HttpOnly = true, SameSite = SameSiteMode.Strict
                };

                Response.Cookies.Append("username", username, usernameCookieOptions);
                Response.Cookies.Append("auth-token", token, usernameCookieOptions); //**Note** As of now the frontend needs the authtoken to not be HttpOnly. If we have the time available research ways we can use this cookie in frontend while set to httpOnly=true.
                output["status"]  = "ok";
                output["message"] = "Sign in successful";
            }
            catch (UserDoesNotExistException) {
                output["status"]    = "error";
                output["errorCode"] = ErrorTypes.BadUsername.ToString("G");
                output["message"]   = "Username does not exist";
            }
            catch (IncorrectPasswordException)
            {
                output["status"]    = "error";
                output["errorCode"] = ErrorTypes.BadPassword.ToString("G");
                output["message"]   = "Incorrect or otherwise bad password";
            }
            catch (BadUsernameException)
            {
                output["status"]    = "error";
                output["errorCode"] = ErrorTypes.BadUsername.ToString("G");
                output["message"]   = "Username is formatted poorly";
            }
            catch (Exception e)
            {
                output["status"]    = "error";
                output["errorCode"] = ErrorTypes.Unknown.ToString("G");
                output["message"]   = "Unknown Error Encountered of type: " + e.GetType();
            }

            return(JsonConvert.SerializeObject(output, Formatting.Indented));
        }
        private void GetFormModel(string surveyid, int UserId, Guid UserId1, out int OrgnizationId, out FormModel FormModel)
        {
            FormModel = new Models.FormModel();
            FormModel.UserHighestRole = int.Parse(Session["UserHighestRole"].ToString());
            // Get OrganizationList
            OrganizationRequest Request = new OrganizationRequest();

            Request.UserId   = UserId;
            Request.UserRole = FormModel.UserHighestRole;
            OrganizationResponse Organizations = _isurveyFacade.GetOrganizationsByUserId(Request);

            FormModel.OrganizationList = Mapper.ToOrganizationModelList(Organizations.OrganizationList);
            //Get Forms
            OrgnizationId      = Organizations.OrganizationList[0].OrganizationId;
            FormModel.FormList = GetFormsInfoList(UserId1, OrgnizationId);
            // Set user Info

            FormModel.UserFirstName = Session["UserFirstName"].ToString();
            FormModel.UserLastName  = Session["UserLastName"].ToString();
            FormModel.SelectedForm  = surveyid;
        }
Example #3
0
        public string signup([FromForm] Models.FormModel user)
        {
            var output = new Dictionary <string, string>();

            try
            {
                var username = user.Username;
                var password = user.Password;
                _ls.SignUp(username, password);

                output["status"]  = "ok";
                output["message"] = "Sign up successful";
            }
            catch (UserAlreadyExistsException)
            {
                output["status"]    = "error";
                output["errorCode"] = ErrorTypes.UserAlreadyExists.ToString("G");
                output["message"]   = "Username already exists";
            }
            catch (BadPasswordException)
            {
                output["status"]    = "error";
                output["errorCode"] = ErrorTypes.BadPassword.ToString("G");
                output["message"]   = "Password is formatted poorly. Password requirements: 8 characters, 1 uppercase, 1 lowercase, 1 special character.";
            }
            catch (BadUsernameException)
            {
                output["status"]    = "error";
                output["errorCode"] = ErrorTypes.BadUsername.ToString("G");
                output["message"]   = "Username is formatted poorly";
            }
            catch (Exception e)
            {
                output["status"]    = "error";
                output["errorCode"] = ErrorTypes.Unknown.ToString("G");
                output["message"]   = "Unknown Error Encountered of type: " + e.GetType();
            }

            return(JsonConvert.SerializeObject(output, Formatting.Indented));
        }
        private FormModel GetFormModel(string surveyId, int userId, Guid userIdGuid, out int orgnizationId)
        {
            FormModel formModel = new Models.FormModel();

            formModel.UserHighestRole = GetIntSessionValue(UserSession.Key.UserHighestRole);
            // Get OrganizationList
            OrganizationRequest request = new OrganizationRequest();

            request.UserId   = userId;
            request.UserRole = formModel.UserHighestRole;
            OrganizationResponse organizations = _securityFacade.GetOrganizationsByUserId(request);

            formModel.OrganizationList = organizations.OrganizationList.ToOrganizationModelList();
            //Get Forms
            orgnizationId      = organizations.OrganizationList[0].OrganizationId;
            formModel.FormList = GetFormsInfoList(userIdGuid, orgnizationId);
            // Set user Info

            formModel.UserFirstName = GetStringSessionValue(UserSession.Key.UserFirstName);
            formModel.UserLastName  = GetStringSessionValue(UserSession.Key.UserLastName);
            formModel.SelectedForm  = surveyId;
            return(formModel);
        }
        public ActionResult Index(Models.FormModel form)
        {
            //make sure there is some form of input
            if (!ModelState.IsValid)
            {
                return(View("~/Views/Home/PleaseTryAgain.cshtml"));
            }

            string keywords = form.Keywords;

            //spaces must be replaced with +, for use in google search query
            keywords = keywords.Replace(" ", "+");
            string url = form.Url;

            //create http request
            HttpClient client = new HttpClient();
            //directly produce only the first 100 results with &num=100
            var response = client.GetStringAsync($"https://www.google.com/search?q= {keywords} &num=100");

            //parse html content with regular expression to find urls
            var r = Regex.Matches(response.Result, @"href\s*=\s*(?:[""'](?<1>[^""']*)[""']|(?<1>\S+))", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            List <string> ListOfUrls = new List <string>();

            //iterate through regex matches and populate the list
            foreach (Match m in r)
            {
                //filter out urls which are not part of the results rankings, all google results start with /url?q=
                string validLink = "/url?q=";
                if (m.Groups[1].Value.StartsWith(validLink))
                {
                    ListOfUrls.Add(m.Groups[1].Value);
                }
            }

            List <int> SearchResult = new List <int>();

            //iterate through list of urls and retrieve rank of specified url
            for (int i = 0; i < ListOfUrls.Count; i++)
            {
                if (ListOfUrls[i].Contains(url))
                {
                    //add 1 to list to show actual ranking and not 0 based list
                    SearchResult.Add(i + 1);
                }
            }

            //convert list to a single string for ease of use with ViewBag
            SearchResult.ConvertAll(delegate(int i) { return(i.ToString()); });
            string searchConverted = string.Join(",", SearchResult);

            ViewBag.searchConverted = searchConverted;

            //if no results have been found, display the PleaseTryAgain view. Otherwise redirect to SearchResult view
            bool check = string.IsNullOrEmpty(searchConverted);

            if (check == false)
            {
                return(View("~/Views/Home/SearchResult.cshtml"));
            }
            else
            {
                return(View("~/Views/Home/PleaseTryAgain.cshtml"));
            }
        }