Wednesday, 31 July 2019

Remove unwanted content from Response header from IIS

///////////////////////// web.config


<system.webServer>
    <httpProtocol>
      <customHeaders>
        <clear />

      <!--Remove headers from from iis -->
        <remove name="Allow" />
        <remove name="Server" />
        <remove name="X-Powered-By" />
        <remove name="X-AspNet-Version" />
        <remove name="Cookie" />

        <!--Add extra headers from security in iis -->
        <add name="X-Frame-Options" value="DENY" />
        <add name="X-XSS-Protection" value="1; mode=block" />
        <add name="X-Content-Type-Options" value="nosniff" />
        <add name="Strict-Transport-Security" value="max-age=31536000" />
        <add name="Cache-Control" value="no-cache, no-store" />
        <add name="Pragma" value="no-cache" />
        <add name="Expires" value="-1" />
      </customHeaders>
     
     <!--<remove name="Content-Length" />
     <remove name="Connection" />
     <remove name="Content-Type" />
     <remove name="Cache-Control" />
     <remove name="Date" />-->
    </httpProtocol>
    </system.webServer>


<!--Rewrite server name in iis -->
<system.webServer>

  <rewrite>
      <outboundRules>
        <rule name="Strip Headers">
          <match serverVariable="RESPONSE_SERVER" pattern=".*" />
          <action type="Rewrite" value="ValueYouWant" replace="true" />
          <conditions>
          </conditions>
        </rule>
      </outboundRules>
    </rewrite>
  </system.webServer>

<!--verb TRACE in iis -->
<system.webServer>
<security>
      <requestFiltering>
        <verbs>
          <add verb="TRACE" allowed="false" />
        </verbs>
      </requestFiltering>

    </security></system.webServer>


<!--Set cookies secure with ssl in iis -->
<system.web>
       <httpCookies httpOnlyCookies="true" requireSSL="true" />
  </system.web>


<!--Set cookies secure with ssl in iis -->
<system.webServer>
    <rewrite>
      <outboundRules>
        <rule name="Use only secure cookies" preCondition="Unsecured cookie">
          <match serverVariable="RESPONSE_SET_COOKIE" pattern=".*" negate="false" />
          <action type="Rewrite" value="{R:0}; secure" />
        </rule>
        <preConditions>
          <preCondition name="Unsecured cookie">
            <add input="{RESPONSE_SET_COOKIE}" pattern="." />
            <add input="{RESPONSE_SET_COOKIE}" pattern="; secure" negate="true" />
          </preCondition>
        </preConditions>
      </outboundRules>
    </rewrite>

  </system.webServer>







<!--Allow version compatibility in iis -->
<system.web>

<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
</system.web>






Monday, 29 July 2019

Remove Duplicate Entry from Comma Delimited String

//////////// Sql Function


Create FUNCTION RemoveDuplicateFrmStr
(
@Str VARCHAR(MAX),
@SplitBy CHAR
)
RETURNS
VARCHAR(MAX)
AS
BEGIN
DECLARE @tblStr TABLE
(
Item VARCHAR(MAX)
)
DECLARE @StrS VARCHAR(MAX), @Pos INT, @StrF VARCHAR(MAX)
SET @Str = LTRIM(RTRIM(@Str)) + @SplitBy
SET @pos = CHARINDEX(@SplitBy, @Str, 1)
WHILE @pos > 0
BEGIN
SET @StrS = LTRIM(RTRIM(LEFT(@Str, @pos - 1)))
IF @StrS <> ''
INSERT INTO @tblStr VALUES (CAST(@StrS AS VARCHAR(MAX)))
SET @Str = SUBSTRING(@Str, @pos+1, LEN(@Str))
SET @pos = CHARINDEX(@SplitBy, @Str, 1)
END
SELECT @StrF = COALESCE(@StrF+@SplitBy+' ','')+ item FROM (SELECT DISTINCT Item FROM tblStr)t


RETURN @StrF
END




---------------call-

declare @str varchar(max)='this is is the the most important thing thing'
declare @str1 varchar(max)='this, is, is, the, the, most, important, thing, thing'
select (dbo.RemoveDuplicateFrmStr(@str,' '))em

select (dbo.RemoveDuplicateFrmStr(@str1,','))em


--------------- Result

1. important  is  most  the  thing  this
2. important, is, most, the, thing, this


Sunday, 21 July 2019

Send email using Godaddy asp.net c# from gmail account

///////////////////////
                         ///////////////////  Godaddy

public static void Email_Send_Godady(string Htmlbody, string mSubject)
{
  try
   {
       string[] Credentials = { "Yourmail@gmail.com", "password" };

       MailMessage msgs = new MailMessage();
       msgs.To.Add("ToEmail id");
       msgs.From = new MailAddress(Credentials[0], "Sender Name"); ;
       msgs.Subject = mSubject;
       msgs.Body = Htmlbody;
       msgs.IsBodyHtml = true;
       SmtpClient client = new SmtpClient();
       client.Credentials = new System.Net.NetworkCredential(Credentials[0], Credentials[1]);
       client.Host = "relay-hosting.secureserver.net";
       client.Port = 25;
       client.EnableSsl = false;
       client.UseDefaultCredentials = false;
       client.Send(msgs);
    }
    catch (Exception ex)
    {
       string msg = ex.ToString();
    }

}


                                    ///////////////////  Gmail


public static void Email_Send_Godady(string Htmlbody, string mSubject)
{
  try
   {
       string[] Credentials = { "Yourmail@gmail.com""password" };

       MailMessage msgs = new MailMessage();
       msgs.To.Add("ToEmail id");
       msgs.From = new MailAddress(Credentials[0], "Sender Name"); ;
       msgs.Subject = mSubject;
       msgs.Body = Htmlbody;
       msgs.IsBodyHtml = true;
       SmtpClient client = new SmtpClient();
       client.Credentials = new System.Net.NetworkCredential(Credentials[0], Credentials[1]);
       client.Host = "smtp.gmail.com";
       client.Port = 587;
       client.EnableSsl = true;
       client.UseDefaultCredentials = false;
       client.Send(msgs);
    }
    catch (Exception ex)
    {
       string msg = ex.ToString();
    }



}

Saturday, 20 July 2019

Encryption and Decryption for Password in C#





Encryption / Decryption:

Encryption is the process of translating plain text data into something that appears to be random and meaningless (ciphertext). 

Decryption is the process of converting ciphertext back to plaintext. To encrypt more than a small amount of data, symmetric encryption is used.

Here we have used Rfc2898DeriveBytes class derived from DeriveBytes.

Namespace:

using System.Security.Cryptography;

C# Code:


Generate Key:

       public static string GenerateEncryptionKey()
        {
            string EncryptionKey = string.Empty;
            Random Robj = new Random();
            int Rnumber = Robj.Next();
            EncryptionKey = "XYZ" + Convert.ToString(Rnumber);
            return EncryptionKey;
        }

The GenerateEncryptionKey() method will generate the key. Pass the generated EncryptionKey to the Encrypt() method to get the Encrypted string value.
or
you can use encryption key inside the method.
 .

For Encryption:
public static readonly string PwdToken = "AnT";

       public static string Password_Encrypt(string clearText, bool IsToken)
        {
            /////////// Generate Token
            string EncryptionKey = "";
            if (IsToken)
            {
                Random Robj = new Random();
                int Rnumber = Robj.Next();
                EncryptionKey = PwdToken + Convert.ToString(Rnumber);
            }

            /////////// Encrypt password
            byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
            using (System.Security.Cryptography.Aes encryptor = System.Security.Cryptography.Aes.Create())
            {
                System.Security.Cryptography.Rfc2898DeriveBytes pdb = new System.Security.Cryptography.Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, encryptor.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length);
                        cs.Close();
                    }
                    clearText = Convert.ToBase64String(ms.ToArray());
                }
            }
            return clearText;
        }


The above method will accept the arguement cleartText and generated encryption key.


For Decryption:


While decrypt the encrypted string, have to pass the EncryptionKey which we have used for encryption.


public static string Password_Decrypt(string cipherText, bool IsToken)
        {
            /////////// Generate Token
            string EncryptionKey = "";
            if (IsToken)
            {
                Random Robj = new Random();
                int Rnumber = Robj.Next();
                EncryptionKey = PwdToken + Convert.ToString(Rnumber);
            }

            /////////// Decrypt password
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            using (System.Security.Cryptography.Aes encryptor = System.Security.Cryptography.Aes.Create())
            {
                System.Security.Cryptography.Rfc2898DeriveBytes pdb = new System.Security.Cryptography.Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, encryptor.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                        cs.Close();
                    }
                    cipherText = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return cipherText;
        }
Usage:

      private void Form_Load(object sender, EventArgs e)
        {

            
string EncryptionKey = GenerateEncryptionKey();

               
/////////// or

            
string EncryptPassword_Encrypt ("YourPassword"true );
            
string DecryptPassword_Decrypt (("YourEncryptedPassword"true );
        }

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...