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;
        } 

Anonymous methods in C#?

What is anonymous method in C#?

A method without a name is called as an anonymous method is, the delegate operator creates an anonymous method that can be converted to a delegate type.

What is use of anonymous methods in C#?

  • You can use an anonymous method to create an anonymous function with unnamed inline code.
  • C# 2.0 introduced the concept of anonymous methods to write unnamed inline statement blocks that can be executed in a delegate invocation.

How to create and use anonymous method?

An anonymous method behaves like a regular method and allows us to write inline code in place of explicitly named methods.

Code example

//declare delegate
delegate void displayFullName(string fname, string mName, string lName);
 
//instantiate delegate with anonymous method
displayFullName fullName = delegate (string fname, string mName, string lName) {
    Console.WriteLine("Full name: " + fname + " " + mName + " " + lName);
   };


 
//anonymous method with generic action delegate
Action<string, stringstring> userFullName = delegate (string fname, string mName, string lName)
{
  Console.WriteLine("User full name: " + fname + " " + mName + " " + lName);
};