Strong Password Generator in C#

This code snippets will let you auto-generate strong passwords. Look for the password rules in the inline comments in code

001 using System;
002 using System.Collections.Generic;
003 using System.Linq;
004 using System.Security.Cryptography.X509Certificates;
005 using System.Text;
006 using System.Threading.Tasks;
007
008 namespace PwdGen
009 {
010     class Program
011     {
012         static void Main(string[] args)
013         {
014             Console.WriteLine(GeneratePassword(815));
015             Console.ReadKey();
016         }
017
018         static string GeneratePassword(int MinLengthint MaxLength)
019         {
020             string ValidChars "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:;<>|=.,_-~!?&%@#$£€°^*§()+[] ";
021             string SpecialChars "!@#$%^&*()";
022             
023             string pwd string.Empty;
024
025             Random rnd = new Random(Guid.NewGuid().GetHashCode());
026             
027             bool NumExist false;
028             bool IsUpper false;
029             bool IsLower false;
030             bool IsSplChr false;
031             bool NoRepeat true;
032             bool NoSeq true;
033
034             while ((!NumExist || !IsUpper || !IsLower || !IsSplChr) && (NoRepeat || NoSeq) )
035             {
036                 NumExist false;
037                 IsUpper false;
038                 IsLower false;
039                 IsSplChr false;
040                 NoRepeat true;
041                 NoSeq true;
042                 
043                 pwd string.Join(string.Empty, Enumerable.Repeat(ValidCharsrnd.Next(MinLengthMaxLength 1)).Select(=> s[rnd.Next(s.Length)]).ToArray());
044                 for (int i 0pwd.Lengthi++)
045                 {
046                     // Contains at least 1 lower case letter and 1 upper case letter (all UTF-8), at least 1 number
047
048                     if (!NumExist)
049                     {
050                         NumExist = (char.IsDigit(pwd[i]) && true);
051                     }
052                     if (!IsUpper)
053                     {
054                         IsUpper = (char.IsUpper(pwd[i]) && true);
055                     }
056                     if (!IsLower)
057                     {
058                         IsLower = (char.IsLower(pwd[i]) && true);
059                     }
060
061                     // A predefined set of special chars must be present
062                     if (!IsSplChr)
063                     {
064                         IsSplChr = (SpecialChars.IndexOf(pwd[i]) >= 0);
065                     }
066
067
068                     // Not more than 2 identical characters in a row (e.g., 111 not allowed)
069                     if (pwd.Length && NoRepeat)
070                     {
071                         NoRepeat = !((pwd[i] == pwd[1]) && (pwd[i] == pwd[2]));
072                     }
073
074                     // Not any sequence of the English alphabet / numbers (above 3 letters)
075                     if (pwd.Length && NoSeq)
076                     {
077                         NoSeq = !((pwd[2] - pwd[1]) == (pwd[1] - pwd[i]));
078                     }
079                     Console.WriteLine(!NumExist +" "+ !IsUpper " " + !IsLower " " + !IsSplChr " " +  NoRepeat +NoSeq);
080                 }
081                 
082             }
083             
084             return pwd;
085
086         }
087     }
088 }
089

View in GitHub | Made with wp-showgithubfile plugin

Join two XML files using XSL Transformation

This blog demonstrates how you can JOIN (yes, like in SQL) two xml (datasets) to one using a common ID (relationship).

shop.xml


<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='main.xsl'?>
<shop>
<product>
<id>100</id>
<title>hello</title>
</product>

<product>
<id>101</id>
<title>world</title>
</product>
<product>
<id>102</id>
<title>praveen</title>
</product>
</shop>

price.xml

<?xml version="1.0" encoding="UTF-8"?>
<shop>
<product>
<id>100</id>
<price>10.0</price>
</product>
<product>
<id>101</id>
<price>10.1</price>
</product>
<product>
<id>102</id>
<price>10.2</price>
</product>
<product>
<id>103</id>
<price>10.3</price>
</product>

</shop>

main.xsl


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

<xsl:param name="fileName" select="'price.xml'" />

<xsl:variable name="updateItems" select="document($fileName)/shop/product" />

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
<xsl:copy-of select="$updateItems[id=current()/id]/price" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Resulting XML, after transformation should look like:


<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='main.xsl'?>
<shop>
<product>
<id>100</id>
<title>hello</title>
<price>10.0</price>
</product>
<product>
<id>101</id>
<title>world</title>
<price>10.1</price>
</product>
<product>
<id>102</id>
<title>praveen</title>
<price>10.2</price>
</product>
</shop>

Bonus code, if some .net developers want a transformation code:


XslTransform xslt = new XslTransform();
xslt.Load(@"D:\Websites\xmltest\main.xsl");
xslt.Transform(@"D:\Websites\xmltest\shop.xml", @"D:\Websites\xmltest\out.xml");
textBox1.Text = System.IO.File.ReadAllText(@"D:\Websites\xmltest\out.xml");

Send an Office365 SMTP mail in .NET/C#

Sending a mail using Office365 is no big deal. It is just similar to how you send a mail via any SMTP server. But you will have to mind the SMTP host, port number and remember to enable SSL in the code. Here goes the code snippet:


using (MailMessage message = new MailMessage())
{
message.From = new MailAddress("praveen@YourO365Domain.com");
message.To.Add("praveen@yourmail.com");
message.Subject = "test mail";
message.Body = "<h1>test</h1>";
message.IsBodyHtml = true;

SmtpClient client = new SmtpClient("outlook.office365.com", 587);

client.Credentials = new NetworkCredential("praveen@YourO365Domain.com", "passwordhere");
client.EnableSsl = true;
client.Send(message);
}