Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

How to check an input against bad or forbidden SQL keywords

Following code sample is not proper prevention for SQL injection this is only preventive approach.

You can try this with Db string values like username, Name, Title etc.

//Detect or Check forbidden SQL keywords 
  public bool sqlKeywrodCheck(string input)   {
  string[] forbiddenKeyWords = new string[] {"INSERT",
            "UPDATE","DELETE", "DROP","TRUNCATE","UNION","SELECT","CREATE"};
  if (forbiddenKeyWords.Any(r => input.IndexOf(r, StringComparison.InvariantCultureIgnoreCase) >= 0))
            {
                return true;
            }
            return false;
        } 

Simple password validation in c#

Following C# method will validate your password with regex and following simple rules

  1. Min length 6 and max 15
  2. Validate for at least one digit, lower case and one upper case

Note : You can change regex if want to add or customize some rules.

public bool IsValidPassword(string input){
    if(string.IsNullOrEmpty(input))
            {
                return false;
            }
    var regex = @"^(?=.{6,15}$)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9]).*$";
    var match = Regex.Match(input, regex, RegexOptions.IgnoreCase);
            if (match.Success)
            {
                return false;
            }
            return true;
        } 

Get data from Rest WebAPI in c#

Consume Web API data using HttpClient in c#

HttpClient is a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.

Following code example get movies data in JSON format,

Web API code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Threading;
using System.Net.Http.Headers;
public class RestAPICall
    {
        private const string URL = "https://c4utest.com/api/movies/search";
        private const string urlParameters = "?Title=Bahubali ";
     
       public void  DisplayMoviesFromRestAPI()
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(URL);
                // List data response.
                HttpResponseMessage response = client.GetAsync(urlParameters).Result; 
                // blocking call
                if (response.IsSuccessStatusCode)
                {
                    // Parse the response body.
                    var dataObjects = response.Content.ReadAsStringAsync().Result; 
                    //data objects
                    Movies deserializedProduct = JsonConvert.DeserializeObject<Movies>(dataObjects);
                    JObject jsonval = JObject.Parse(dataObjects);
                    if (deserializedProduct != null)
                    {
                        Console.WriteLine("Total movies : "+ deserializedProduct.total);
                        Console.WriteLine("Current page : " + deserializedProduct.page);
                        Console.WriteLine("Total pages : " + deserializedProduct.total_pages);
 
                        //first movie details
                        var firstMoviesDetails = deserializedProduct.data.FirstOrDefault();
                        //Display first moview details from collections
                        Console.WriteLine("First movie title : " + firstMoviesDetails.Title);
                        Console.WriteLine("First movie release year : " + firstMoviesDetails.Year);
                        Console.WriteLine("First movie code : " + firstMoviesDetails.imdbID);
 
                        //display all movies details
                        foreach (var movieDetail in deserializedProduct.data)
                        {
                            Console.WriteLine("Movie title : " + movieDetail.Title);
                            Console.WriteLine("Movie release year : " + movieDetail.Year);
                            Console.WriteLine("Movie code : " + movieDetail.imdbID);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                }
            }
        }
    }

Movies class

public class Movies
    {
      public int page { get; set; }
      public int per_page { get; set; }
      public int total { get; set; }
      public int total_pages { get; set; }
      public List<MoviesData> data { get; set; }
    }

MoviesData class 

public class MoviesData
    {
        public string Title { get; set; }
        public int Year { get; set; }
        public string imdbID { get; set; }
    }