Class pour les login

Contenu du snippet

// Une Classe qui peut être utiliser avec vos projets.
// il sagit d'un hacheur Pour Mot de passe [ sha1encrypt ]
// d'un Encodeur / Décodeur de texte en sha512 [ GetEncryptedData | GetDecryptedData ]
// Et aussi le moyen de créer un fichier xml pour sauvegarder vos Nom Utilisateurs mot de passe encrypter
// a peut pres n'import-ou sur votre machine. et de vérifier votre mot de passe.

Source / Exemple :


using System.IO;
using System;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.Security.Cryptography;

namespace Login1
{
    class users
    {
        public string UName = "admin";
        public string UPassword = "admin";

        public void CreateNewPWD()
        {
            XmlDocument docUserLogin = new XmlDocument();
            string strFilename = Application.ProductName + ".xml";
            string UserName = "admin";
            string UPassword = Encrypt("admin");
            //crée le fichier XML
            docUserLogin.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<!-- Copyright Daniel Morais 2009 -->" + "<" + Application.ProductName + "> <Users><Name>" + UserName + "</Name> <Password>" + UPassword + "</Password> </Users> </" + Application.ProductName + ">");
            docUserLogin.Save(strFilename);
            MessageBox.Show("Utilisateur  'admin'  et Mot de passe  'admin' " + "\r\n" +
             "ont été créer pour permettre un premier accès au programme " + "\r\n" +
             "Vous devriez les changés après l'ouverture du programme en cliquand sur " + "\r\n" +
             "Changer le mot de passe.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //Récupération des attributs d'un fichier                  
            File.SetAttributes(strFilename, FileAttributes.Hidden);
        }

        public void SaveUPWord(string strUser, string strPassword)
        {
            XmlDocument docUserLogin = new XmlDocument();
            string strFilename = Application.ProductName + ".xml";
            try
            {
                File.Delete(strFilename);
                //create the xml file
                docUserLogin.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<!-- Copyright Daniel Morais 2009 -->" + "<" + Application.ProductName + "> <Users><Name>" + strUser + "</Name> <Password>" + Encrypt(strPassword) + "</Password> </Users> </" + Application.ProductName + ">");
                docUserLogin.Save(strFilename);
                File.SetAttributes(strFilename, FileAttributes.Hidden);
            }
            catch
            {
                MessageBox.Show("Une erreur s'est produite dans le programme, impossible de connecter a " + strFilename, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            finally
            {
                MessageBox.Show("L'utilisateur et le mot de passe ont été changé avec succès.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        public void ReadUPWord()
        {
            string FILE_NAME = Application.ProductName + ".xml";
            XPathDocument doc;
            XPathNavigator nav;
            XPathExpression expr;
            XPathNodeIterator iterator;

            //LoadXml(FILE_NAME);
            doc = new XPathDocument(FILE_NAME);
            nav = doc.CreateNavigator();

            // Compile a standard XPath expression

            expr = nav.Compile("/" + Application.ProductName + "/Users/Name");
            iterator = nav.Select(expr);
            while (iterator.MoveNext())
            {
                XPathNavigator nav1 = iterator.Current.Clone();
                UName = nav1.Value;
            }
            expr = nav.Compile("/" + Application.ProductName + "/Users/Password");
            iterator = nav.Select(expr);
            while (iterator.MoveNext())
            {
                XPathNavigator nav2 = iterator.Current.Clone();
                UPassword = nav2.Value;
            }
        }

        public string Encrypt(string pwd)
        {
            UTF8Encoding encoder = new UTF8Encoding();
            SHA1CryptoServiceProvider sha1hasher = new SHA1CryptoServiceProvider();
            byte[] hashedDataBytes = sha1hasher.ComputeHash(encoder.GetBytes(pwd));
            return byteArrayToString(hashedDataBytes);
        }

        private string byteArrayToString(byte[] inputArray)
        {
            StringBuilder output = new StringBuilder("");
            for (int i = 0; i < inputArray.Length; i++)
            {
                output.Append(inputArray[i].ToString("X2"));
            }
            return output.ToString();
        }

        ///'******* Encrypt the Data *******
        private string GetEncryptedData(string Data)
        {
            SHA512Managed shaM = new SHA512Managed();
            Convert.ToBase64String(shaM.ComputeHash(Encoding.ASCII.GetBytes(Data)));
            byte[] eNC_data = ASCIIEncoding.ASCII.GetBytes(Data);
            string eNC_str = Convert.ToBase64String(eNC_data);
            //GetEncryptedData = eNC_str;
            return eNC_str;
        }

        ///'******* Decrypt the Data *******
        private string GetDecryptedData(string Data)
        {
            byte[] dEC_data = Convert.FromBase64String(Data);
            string dEC_Str = ASCIIEncoding.ASCII.GetString(dEC_data);
            //GetDecryptedData = dEC_Str;
            return dEC_Str;
        }

    }

}

Conclusion :


users objuser = new users();
Pour utiliser le hacheur - Encrypt(UPassword)
pour l'encodeur - rtftext.Text = GetEncryptedData(rtftext.Rtf)
pour le décodeur - rtftext.rtf = GetDecryptedData(rtftext.Text)
pour créer le fichier XML - CreateNewPWD() ;
pour le lire - ReadPWord() ;
Bien sure il va vous falloir utiliser vos propre mot et textbox pour vous facilité la tâche

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.