Click here to Skip to main content
Click here to Skip to main content

Complete Users' Password Administration for .NET

, 24 Jun 2005
Rate this:
Please Sign up or sign in to vote.
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.

    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.

    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).

    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.

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

History

None.

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

Share

About the Author

arbel kfir
Web Developer
Israel Israel
partying, girls, partying, girls
 
and a bit of c#
 
homepage : http:\\www.myi4u.com

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 24 Jun 2005
Article Copyright 2005 by arbel kfir
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid