Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / XML

Complete Users' Password Administration for .NET

Rate me:
Please Sign up or sign in to vote.
2.31/5 (7 votes)
24 Jun 20052 min read 26.9K   1.1K   16   2
This article creates a complete suite to manage users and users' passwords. The application saves and loads all the data using an XML file and MD5 encryption.

Sample Image

Introduction

I have looked for a simple way to save user data to an XML file. All the solutions I have found used XML serialization (=> not so simple). So I have decided to create my own application. The data in the XML file must be encoded for security reason.

The following subjects are discussed in this article:

  1. Encode string using MD5 encoding.
  2. Load all user database from the XML file to the application.
  3. Save all user database to an XML file.
  4. Look for an object in an ArrayList.

Background

To change the Desktop background, the application needs to use the WinAPI SystemParametersInfo and supply it with the desired image parameters. The only problem is that the API accepts only BMP image type. My solution was to create a BMP from each JPG or JPEG file in the directory.

Using the Code

  1. Encode string using MD5 encoding:

    In order to do the encoding, this application creates a MD5 Encoding object. Using StringBuilder, the string is then converted into an array of bytes and converted back to string after the MD5 encoding.

    C#
    MD5 md5serv = MD5CryptoServiceProvider.Create();
    byte[] hash;
    StringBuilder stringbuff = new StringBuilder();
    ASCIIEncoding asciienc = new ASCIIEncoding();
    
    // convert string into array of bytes
    byte[] buffer = asciienc.GetBytes(raw);
    
    // Comupte the hash for the string
    hash = md5serv.ComputeHash(buffer);
    foreach (byte b in hash) { stringbuff.Append(b.ToString("x2")); }
    return stringbuff.ToString();
  2. Load all user database from the XML file to the application:

    The method used reads the XML file into a XmlTextReader. Then it searches for new XML elements. For each element, the method extracts its attributes. Attributes are extracted from reader.Value.

    C#
    while (reader.Read()) {
        if (reader.NodeType == XmlNodeType.Element) {
            elementName = reader.Name;
            while (reader.MoveToNextAttribute()) {
                if (reader.Name == "Password")
                    myUser.Password = reader.Value;
                }
            }
        }
  3. Save all user database to an XML file:

    Save all user database to an XML file from the application. The method used here uses the XmlTextWriter. It saves all the users to a new XML file. First of all, the XML options must be set - like formatting and indentation. Then it writes the start element. After that, for each user, the method adds its attributes (XML attributes) to the user name (XML element).

    C#
    XmlTextWriter writer = new XmlTextWriter(strPath + "\\users.xml", null);
    writer.Formatting = Formatting.Indented;
    writer.Indentation = 4;
    writer.WriteStartElement("Users");
    foreach (User myUser in myAL) {
        for each user (myUser.UserName);
        writer.WriteAttributeString("Password", myUser.Password);
        writer.WriteAttributeString("Previliage", myUser.Previliage);
        writer.WriteEndElement();
    }
    writer.Flush();
    writer.Close();
  4. Remove an object from an ArrayList:

    If you want to delete an object from an ArrayList, you don't need any index. All you need is to find the correct pointer and delete it.

    C#
    foreach (User myUser in myAL) {
        if (myUser.UserName == (string)listBoxUser.SelectedItem)
            myAL.Remove(myUser);
        }
    }

History

  • 24th June, 2005: Initial version

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.


Written By
Web Developer
Israel Israel
partying, girls, partying, girls

and a bit of c#

homepage : http:\\www.myi4u.com

Comments and Discussions

 
GeneralNeeds some work Pin
Frank McCown7-Oct-05 1:06
Frank McCown7-Oct-05 1:06 
GeneralMr. Webster "writ" a mighty fine dictionary Pin
fwsouthern24-Jun-05 9:52
fwsouthern24-Jun-05 9:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.