讓ERP系統記住登入的密碼

來源:互聯網
上載者:User

在實際應用中,使用者可能需要ERP記錄密碼,不想每次登入的時候都輸入,就像qq一樣.

下面是C#的代碼:

先引用以下命名空間

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Management;using System.Windows.Forms;using System.IO;using System.Xml;using System.Xml.Linq;

具體代碼如下:

 /// <summary>        /// 儲存登入密碼        /// </summary>        /// <param name="diskid">硬碟id</param>        /// <param name="deptid">部門id</param>        /// <param name="uid">使用者名稱</param>        /// <param name="pwd">密碼</param>        /// <param name="IsSave">是否儲存</param>        /// <returns></returns>        public bool SavePassword(string diskid, int deptid, string uid, string pwd, bool IsSave)        {            bool flag = false;            try            {                string xmlfile = Application.StartupPath + "\\user.xml";                if (!File.Exists(xmlfile))                {                    XDocument xdoc = new XDocument();                    xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes")                                         , new XElement("Users"                                              , new XAttribute("Id", diskid)));                    xdoc.Save(xmlfile);                }                XDocument doc = XDocument.Load(xmlfile);                XElement xe = doc.Element("Users");                if (diskid == xe.Attribute("Id").Value.ToString())                {                    //先尋找是否已經儲存這個使用者                    var pwds = from un in xe.Descendants("Dept")                               where un.Attribute("Id").Value == deptid.ToString()                                 && un.Attribute("UserName").Value == uid                               select un;                    //如果是儲存則修改尋找到的使用者名稱、密碼或添加使用者名稱、密碼節點                    if (IsSave)                    {                        if (pwds.Count() > 0)                        {                            foreach (var node in pwds)                            {                                node.Attribute("PassWord").Value = pwd;                            }                        }                        else                        {                            XElement xel = new XElement("Dept");                            xel.Add(new XAttribute("Id", deptid));                            xel.Add(new XAttribute("UserName", uid));                            xel.Add(new XAttribute("PassWord", pwd));                            xe.Add(xel);                        }                    }                    else                    {                        if (pwds.Count() > 0)                        {                            //刪除儲存的帳號及密碼                            XElement node = (XElement)pwds.SingleOrDefault();                            node.Remove();                        }                                          }                    doc.Save(xmlfile);                }            }            catch (Exception ex)            {                flag = false;            }            return flag;        }

下面是讀取使用者的密碼:

/// <summary>        /// 讀取儲存的使用者密碼        /// </summary>        /// <param name="diskid">硬碟id</param>        /// <param name="deptid">部門id</param>        /// <param name="uid">使用者名稱</param>        /// <returns></returns>        public string ReadPassword(string diskid, int deptid, string uid)        {            string pwd = "";            try            {                string xmlfile = Application.StartupPath + "\\user.xml";                if (!File.Exists(xmlfile))                {                    return "";                }                XDocument doc = XDocument.Load(xmlfile);                XElement xe = doc.Element("Users");                //先尋找是否已經儲存這個使用者                var pwds = from un in xe.Descendants("Dept")                           where un.Attribute("Id").Value == deptid.ToString()                             && un.Attribute("UserName").Value == uid                           select un;                if (pwds.Count() > 0)                {                    foreach (var node in pwds)                    {                       pwd= node.Attribute("PassWord").Value ;                    }                }            }            catch (Exception ex)            {                pwd = "";            }            return pwd;        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.