通达OA简易中间件(同时向OA系统和域系统增加登录帐号) .

通达OA简易中间件(同时向OA系统和域系统增加登录帐号) .

通达OA简易中间件分成两部分:

1. OA服务器接口PHP文件

2. 桌面操作程序(exe)


实现原理:从OA获取所有配置信息(省去再一次配置的麻烦),然后通过桌面程序向AD域和OA分别增加帐号。


OA服务器接口PHP文件:负责把OA的数据配置、域配置、以及增加帐号所需要的基础数据回传给 桌机操作程序

PHP接口代码主要是返回配置的数据(部分代码),在桌面程序通过获取的HTML文件,使用正则解释 <option value=值>文本</option>。

[php]

  1. <?php
  2. if($TYPE == "gender")
  3. {
  4. echo "<option value=\"0\">男</option>";
  5. echo "<option value=\"1\">女</option>";
  6. }
  7. ?>
<?php
if($TYPE == "gender")
{
    echo "<option value=\"0\">男</option>";
    echo "<option value=\"1\">女</option>";
}
?>


 桌面操作程序(exe):使用C#开发

www.zeeklog.com  - 通达OA简易中间件(同时向OA系统和域系统增加登录帐号) .

在填写了OA的访问地址后,如 http://192.168.56.101:81/

www.zeeklog.com  - 通达OA简易中间件(同时向OA系统和域系统增加登录帐号) .

窗体使用了异步加载的方法,好较好的用户体验。

初始化,使用代理实现异步操作

[csharp]

  1. //禁用状态
  2. gbBasicInfo.Enabled = false;
  3. bgOtherInfo.Enabled = false;
  4. btnClose.Enabled = false;
  5. btnSubmit.Enabled = false;
  6. lbMessage.Visible = true;
  7. //异步处理加载配置数据
  8. //包括域地址、MySQL帐号等
  9. TransmitDelegate transmit = Transmit;
  10. IAsyncResult asyncResult = transmit.BeginInvoke(new AccountInfo(), TransmitCallBack, transmit);
            //禁用状态
            gbBasicInfo.Enabled = false;
            bgOtherInfo.Enabled = false;
            btnClose.Enabled = false;
            btnSubmit.Enabled = false;
            lbMessage.Visible = true;

            //异步处理加载配置数据
            //包括域地址、MySQL帐号等
            TransmitDelegate transmit = Transmit;
            IAsyncResult asyncResult = transmit.BeginInvoke(new AccountInfo(), TransmitCallBack, transmit);


信息加载主要代码

[csharp]

  1. void Transmit(AccountInfo state)
  2. {
  3. #region 获取OA接口地址
  4. //OA地址判断是否有效
  5. m_oaHttpUrl = FileHelper.ReadFileByFullname(m_config);
  6. if (!Regex.IsMatch(m_oaHttpUrl, @"^http://([^/]+)(/?){1}quot;))
  7. {
  8. frmOAInterface frm = new frmOAInterface(m_config);
  9. frm.ShowDialog(this);
  10. frm.ShowInTaskbar = false;
  11. if (frm.DialogResult == DialogResult.OK)
  12. {
  13. m_oaHttpUrl = frm.OAHttpUrl;
  14. }
  15. else
  16. {
  17. Application.Exit();
  18. }
  19. }
  20. //追加接口地址
  21. if (m_oaHttpUrl.Substring(m_oaHttpUrl.Length - 1) != "/")
  22. {
  23. m_oaHttpUrl += "/";
  24. }
  25. m_oaHttpUrl += "OAMidHelper.php";
  26. #endregion
  27. #region 初始化
  28. Spider spider = new Spider();
  29. string html = "";
  30. MatchCollection mc = null;
  31. IList<OptionValue> ovs = null;
  32. #endregion
  33. try
  34. {
  35. //省略部分代码
  36. #region 获取性别
  37. SetLableMessageValue("获取性别中...");
  38. html = spider.GetFullHtml(m_oaHttpUrl + "?TYPE=gender", "GBK", null);
  39. ovs = new List<OptionValue>();
  40. mc = m_optionRegex.Matches(html);
  41. if (mc.Count == 0)
  42. {
  43. throw new Exception("无法获取性别数据,请重试。");
  44. }
  45. foreach (Match item in mc)
  46. {
  47. ovs.Add(new OptionValue(item.Result("$2"), item.Result("$1")));
  48. }
  49. cbGender.DataSource = ovs;
  50. cbGender.DisplayMember = "Text";
  51. cbGender.ValueMember = "Value";
  52. #endregion
  53. }
  54. catch (Exception ex)
  55. {
  56. MessageBox.Show(this, ex.Message);
  57. }
  58. }
void Transmit(AccountInfo state)
        {
            #region 获取OA接口地址
            //OA地址判断是否有效
            m_oaHttpUrl = FileHelper.ReadFileByFullname(m_config);
            if (!Regex.IsMatch(m_oaHttpUrl, @"^http://([^/]+)(/?){1}quot;))
            {
                frmOAInterface frm = new frmOAInterface(m_config);
                frm.ShowDialog(this);
                frm.ShowInTaskbar = false;

                if (frm.DialogResult == DialogResult.OK)
                {
                    m_oaHttpUrl = frm.OAHttpUrl;
                }
                else
                {
                    Application.Exit();
                }
            }

            //追加接口地址
            if (m_oaHttpUrl.Substring(m_oaHttpUrl.Length - 1) != "/")
            {
                m_oaHttpUrl += "/";
            }
            m_oaHttpUrl += "OAMidHelper.php";
            #endregion

            #region 初始化
            Spider spider = new Spider();
            string html = "";
            MatchCollection mc = null;
            IList<OptionValue> ovs = null;
            #endregion

            try
            {
                //省略部分代码
                #region 获取性别
                SetLableMessageValue("获取性别中...");
                html = spider.GetFullHtml(m_oaHttpUrl + "?TYPE=gender", "GBK", null);

                ovs = new List<OptionValue>();
                mc = m_optionRegex.Matches(html);
                if (mc.Count == 0)
                {
                    throw new Exception("无法获取性别数据,请重试。");
                }
                foreach (Match item in mc)
                {
                    ovs.Add(new OptionValue(item.Result("$2"), item.Result("$1")));
                }
                cbGender.DataSource = ovs;
                cbGender.DisplayMember = "Text";
                cbGender.ValueMember = "Value";
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }



回调成功后,把禁用的对象还原出来:

[csharp]

  1. void TransmitCallBack(IAsyncResult asyncResult)
  2. {
  3. gbBasicInfo.Enabled = true;
  4. bgOtherInfo.Enabled = true;
  5. btnClose.Enabled = true;
  6. btnSubmit.Enabled = true;
  7. lbMessage.Visible = false;
  8. }
        void TransmitCallBack(IAsyncResult asyncResult)
        {
            gbBasicInfo.Enabled = true;
            bgOtherInfo.Enabled = true;
            btnClose.Enabled = true;
            btnSubmit.Enabled = true;
            lbMessage.Visible = false;
        }


向AD域增加帐号的关键代码:

[csharp]

  1. #region 向AD域增加用户
  2. SetLableMessageValue("向AD增加帐号...");
  3. string ouUserName = userID + "(" + userName + ")";
  4. using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + addept))
  5. {
  6. DirectoryEntry entry2 = entry.Children.Add("CN=" + ouUserName, "user");
  7. entry2.Properties["sAMAccountName"].Add(userID);
  8. entry2.Properties["displayname"].Add(ouUserName);
  9. entry2.Properties["userPrincipalName"].Add(userID + "@" + DOMAIN_NAME);
  10. if (telphone.Length > 0)
  11. {
  12. entry2.Properties["telephonenumber"].Add(telphone);
  13. }
  14. entry2.Properties["company"].Add(oadept2);
  15. entry2.Properties["userAccountControl"].Value = 0x200;
  16. entry2.CommitChanges();
  17. entry2.Invoke("SetPassword", new object[] { password });
  18. entry2.CommitChanges();
  19. }
  20. #endregion
                #region 向AD域增加用户
                SetLableMessageValue("向AD增加帐号...");
                string ouUserName = userID + "(" + userName + ")";
                using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + addept))
                {
                    DirectoryEntry entry2 = entry.Children.Add("CN=" + ouUserName, "user");
                    entry2.Properties["sAMAccountName"].Add(userID);
                    entry2.Properties["displayname"].Add(ouUserName);
                    entry2.Properties["userPrincipalName"].Add(userID + "@" + DOMAIN_NAME);
                    if (telphone.Length > 0)
                    {
                        entry2.Properties["telephonenumber"].Add(telphone);
                    }
                    entry2.Properties["company"].Add(oadept2);
                    entry2.Properties["userAccountControl"].Value = 0x200;
                    entry2.CommitChanges();
                    entry2.Invoke("SetPassword", new object[] { password });
                    entry2.CommitChanges();
                }
                #endregion


用于获取远程HTML的关键代码:

[csharp]

  1. /// <summary>
  2. /// 网页文件页头内容正则
  3. /// </summary>
  4. public static Regex ContentTypeTextRegex = new Regex(@"text/(\w+)", RegexOptions.IgnoreCase);
        /// <summary>
        /// 网页文件页头内容正则
        /// </summary>
        public static Regex ContentTypeTextRegex = new Regex(@"text/(\w+)", RegexOptions.IgnoreCase);


   [csharp]

  1. /// <summary>
  2. /// 创建访问URL的Response
  3. /// </summary>
  4. private HttpWebResponse getWebResponse(string uri, Cookie cookie)
  5. {
  6. HttpWebRequest request = null;
  7. HttpWebResponse response = null;
  8. try
  9. {
  10. //请求
  11. request = HttpWebRequest.Create(uri) as HttpWebRequest;
  12. CookieContainer myCookieContainer = new CookieContainer();
  13. request.KeepAlive = true;
  14. request.AllowAutoRedirect = true;
  15. request.AllowWriteStreamBuffering = true;
  16. request.Referer = "http://www.lanxe.net/";
  17. request.Timeout = 20000;
  18. request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215; fqSpider)";
  19. request.CookieContainer = myCookieContainer;
  20. //加入Cookie
  21. if (cookie != null)
  22. {
  23. myCookieContainer.Add(cookie);
  24. }
  25. //回应
  26. response = request.GetResponse() as HttpWebResponse;
  27. response.Cookies = myCookieContainer.GetCookies(request.RequestUri);
  28. if (response.StatusCode == HttpStatusCode.OK)
  29. {
  30. return response;
  31. }
  32. }
  33. catch
  34. {
  35. }
  36. //返回
  37. return null;
  38. }
/// <summary>
        /// 创建访问URL的Response
        /// </summary>
        private HttpWebResponse getWebResponse(string uri, Cookie cookie)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;

            try
            {
                //请求
                request = HttpWebRequest.Create(uri) as HttpWebRequest;
                CookieContainer myCookieContainer = new CookieContainer();

                request.KeepAlive = true;
                request.AllowAutoRedirect = true;
                request.AllowWriteStreamBuffering = true;
                request.Referer = "http://www.lanxe.net/";
                request.Timeout = 20000;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215; fqSpider)";
                request.CookieContainer = myCookieContainer;

                //加入Cookie
                if (cookie != null)
                {
                    myCookieContainer.Add(cookie);
                }

                //回应
                response = request.GetResponse() as HttpWebResponse;
                response.Cookies = myCookieContainer.GetCookies(request.RequestUri);
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return response;
                }
            }
            catch
            {

            }

            //返回
            return null;
        }


   [csharp]

  1. /// <summary>
  2. /// 获取网页源代码
  3. /// </summary>
  4. public string GetFullHtml(string uri, string encodeName, Cookie cookie)
  5. {
  6. HttpWebResponse response = getWebResponse(uri, cookie);
  7. if (response == null)
  8. {
  9. return null;
  10. }
  11. try
  12. {
  13. //HTML容器
  14. StringBuilder outs = new StringBuilder();
  15. //判断文档是否为Text形式
  16. string contentType = response.Headers["Content-Type"].ToLower();
  17. if (ContentTypeTextRegex.IsMatch(contentType))
  18. {
  19. //网页编码
  20. Encoding encode;
  21. if (encodeName.ToLower() == "default")
  22. {
  23. encode = Encoding.Default;
  24. }
  25. else
  26. {
  27. encode = Encoding.GetEncoding(encodeName);
  28. }
  29. Stream receiveStream = response.GetResponseStream();
  30. StreamReader reader = new StreamReader(receiveStream, encode);
  31. // 每次读取1024
  32. char[] read = new char[1024];
  33. int count = reader.Read(read, 0, 1024);
  34. while (count > 0)
  35. {
  36. outs.Append(new String(read, 0, count));
  37. count = reader.Read(read, 0, 1024);
  38. }
  39. // 读取完毕
  40. reader.Close();
  41. receiveStream.Close();
  42. }
  43. return outs.ToString();
  44. }
  45. catch
  46. {
  47. }
  48. return null;
  49. }
/// <summary>
        /// 获取网页源代码
        /// </summary>
        public string GetFullHtml(string uri, string encodeName, Cookie cookie)
        {
            HttpWebResponse response = getWebResponse(uri, cookie);
            if (response == null)
            {
                return null;
            }

            try
            {
                //HTML容器
                StringBuilder outs = new StringBuilder();

                //判断文档是否为Text形式
                string contentType = response.Headers["Content-Type"].ToLower();
                if (ContentTypeTextRegex.IsMatch(contentType))
                {
                    //网页编码
                    Encoding encode;
                    if (encodeName.ToLower() == "default")
                    {
                        encode = Encoding.Default;
                    }
                    else
                    {
                        encode = Encoding.GetEncoding(encodeName);
                    }

                    Stream receiveStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(receiveStream, encode);

                    // 每次读取1024
                    char[] read = new char[1024];
                    int count = reader.Read(read, 0, 1024);
                    while (count > 0)
                    {
                        outs.Append(new String(read, 0, count));
                        count = reader.Read(read, 0, 1024);
                    }

                    // 读取完毕
                    reader.Close();
                    receiveStream.Close();
                }

                return outs.ToString();
            }
            catch
            {

            }

            return null;
        }