纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
7.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. namespace ICSSoft.FromERP
  7. {
  8. public class SRMUser
  9. {
  10. static string conStr = ICSHelper.GetConnectString();
  11. public static void User()
  12. {
  13. string sqlUser = "";
  14. string NewGuid = GuId();
  15. sqlUser = @"INSERT INTO dbo.Sys_SRM_User
  16. ( F_Id ,F_Account ,F_RealName ,F_NickName ,
  17. F_RoleId ,F_IsAdministrator , F_EnabledMark ,
  18. F_CreatorTime ,F_CreatorUserId ,F_Location ,
  19. F_VenCode)
  20. SELECT NEWID(),a.VenCode,a.VenName,a.VenName,
  21. '2691AB91-3010-465F-8D92-60A97425A45E',0,1,
  22. GETDATE(),'9f2ec079-7d0f-4fe2-90ab-8b09a8302aba','6000',''
  23. FROM dbo.ICSVendor a
  24. Left join Sys_SRM_User b on a.VenCode=b.F_Account
  25. where a.VenCode is null
  26. ";
  27. try
  28. {
  29. ICSHelper.ExecuteDate(conStr,sqlUser);
  30. }
  31. catch (Exception ex)
  32. {
  33. throw new Exception(ex.Message);
  34. }
  35. string UserSecretkey = Md5.md5(CreateNo(), 16).ToLower();
  36. string pwd = Md5.md5(DESEncrypt.Encrypt(Md5.md5("123456", 32).ToLower(), UserSecretkey).ToLower(), 32).ToLower();
  37. sqlUser = @"INSERT INTO dbo.Sys_SRM_UserLogOn
  38. ( F_Id ,F_UserId ,F_UserPassword ,F_UserSecretkey )
  39. SELECT a.F_Id,a.F_Id,'" + pwd + "','" + UserSecretkey + "'from Sys_SRM_User a left join Sys_SRM_UserLogOn b on a.F_Id=b.F_Id where a.F_Id is null ";
  40. try
  41. {
  42. ICSHelper.ExecuteDate(conStr, sqlUser);
  43. }
  44. catch (Exception ex)
  45. {
  46. throw new Exception(ex.Message);
  47. }
  48. }
  49. #region 自动生成编号
  50. /// <summary>
  51. /// 表示全局唯一标识符 (GUID)。
  52. /// </summary>
  53. /// <returns></returns>
  54. public static string GuId()
  55. {
  56. return Guid.NewGuid().ToString();
  57. }
  58. #endregion
  59. /// <summary>
  60. /// 自动生成编号 201008251145409865
  61. /// </summary>
  62. /// <returns></returns>
  63. public static string CreateNo()
  64. {
  65. Random random = new Random();
  66. string strRandom = random.Next(1000, 10000).ToString(); //生成编号
  67. string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom;//形如
  68. return code;
  69. }
  70. /// <summary>
  71. /// MD5加密
  72. /// </summary>
  73. public class Md5
  74. {
  75. /// <summary>
  76. /// MD5加密
  77. /// </summary>
  78. /// <param name="str">加密字符</param>
  79. /// <param name="code">加密位数16/32</param>
  80. /// <returns></returns>
  81. public static string md5(string str, int code)
  82. {
  83. string strEncrypt = string.Empty;
  84. if (code == 16)
  85. {
  86. strEncrypt = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").Substring(8, 16);
  87. }
  88. if (code == 32)
  89. {
  90. strEncrypt = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
  91. }
  92. return strEncrypt;
  93. }
  94. }
  95. public class DESEncrypt
  96. {
  97. private static string DESKey = "nfine_desencrypt_2016";
  98. #region ========加密========
  99. /// <summary>
  100. /// 加密
  101. /// </summary>
  102. /// <param name="Text"></param>
  103. /// <returns></returns>
  104. public static string Encrypt(string Text)
  105. {
  106. return Encrypt(Text, DESKey);
  107. }
  108. /// <summary>
  109. /// 加密数据
  110. /// </summary>
  111. /// <param name="Text"></param>
  112. /// <param name="sKey"></param>
  113. /// <returns></returns>
  114. public static string Encrypt(string Text, string sKey)
  115. {
  116. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  117. byte[] inputByteArray;
  118. inputByteArray = Encoding.Default.GetBytes(Text);
  119. des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  120. des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  121. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  122. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  123. cs.Write(inputByteArray, 0, inputByteArray.Length);
  124. cs.FlushFinalBlock();
  125. StringBuilder ret = new StringBuilder();
  126. foreach (byte b in ms.ToArray())
  127. {
  128. ret.AppendFormat("{0:X2}", b);
  129. }
  130. return ret.ToString();
  131. }
  132. #endregion
  133. #region ========解密========
  134. /// <summary>
  135. /// 解密
  136. /// </summary>
  137. /// <param name="Text"></param>
  138. /// <returns></returns>
  139. public static string Decrypt(string Text)
  140. {
  141. if (!string.IsNullOrEmpty(Text))
  142. {
  143. return Decrypt(Text, DESKey);
  144. }
  145. else
  146. {
  147. return "";
  148. }
  149. }
  150. /// <summary>
  151. /// 解密数据
  152. /// </summary>
  153. /// <param name="Text"></param>
  154. /// <param name="sKey"></param>
  155. /// <returns></returns>
  156. public static string Decrypt(string Text, string sKey)
  157. {
  158. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  159. int len;
  160. len = Text.Length / 2;
  161. byte[] inputByteArray = new byte[len];
  162. int x, i;
  163. for (x = 0; x < len; x++)
  164. {
  165. i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  166. inputByteArray[x] = (byte)i;
  167. }
  168. des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  169. des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  170. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  171. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  172. cs.Write(inputByteArray, 0, inputByteArray.Length);
  173. cs.FlushFinalBlock();
  174. return Encoding.Default.GetString(ms.ToArray());
  175. }
  176. #endregion
  177. }
  178. }
  179. }