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.

318 lines
12 KiB

  1. using Swift.API.Common;
  2. using Swift.API.GoogleAuthenticator;
  3. using Swift.DAL.BL.System.UserManagement;
  4. using Swift.DAL.SwiftDAL;
  5. using Swift.web.Library;
  6. using Swift.web.SwiftSystem.UserManagement.ApplicationUserPool;
  7. using System;
  8. using System.Text;
  9. using System.Web;
  10. using System.Web.Script.Serialization;
  11. namespace Swift.web.Admin
  12. {
  13. public partial class Default : System.Web.UI.Page
  14. {
  15. private ApplicationUserDao user = new ApplicationUserDao();
  16. protected GoogleAuthenticatorAPI _auth = new GoogleAuthenticatorAPI();
  17. private string ipAddress = "";
  18. private string Username = "";
  19. private string pwd = "";
  20. private string Usercode = "";
  21. protected string use2FA = "Y";
  22. private UserPool userPool = UserPool.GetInstance();
  23. protected void Page_Load(object sender, EventArgs e)
  24. {
  25. ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  26. if (string.IsNullOrEmpty(ipAddress))
  27. {
  28. ipAddress = Request.ServerVariables["REMOTE_ADDR"];//"203.223.132.106";//
  29. }
  30. if (!IsPostBack)
  31. {
  32. if (GetStatic.ReadWebConfig("UseGoogle2FAuthAdmin", "") != "Y")
  33. {
  34. use2FA = "N";
  35. DisableGoogle2FAuth();
  36. }
  37. else
  38. {
  39. EnableGoogle2FAuth();
  40. }
  41. string methodname = Request.Form["methodName"];
  42. switch (methodname)
  43. {
  44. case "GetLogin":
  45. GetLogin();
  46. break;
  47. }
  48. }
  49. //userName.Attributes.Add("onkeypress", "ClearMessage()");
  50. //pwd.Attributes.Add("onkeypress", "isCapslock((event?event:evt))");
  51. //userCode.Attributes.Add("onkeypress", "ClearMessage()");
  52. }
  53. private void EnableGoogle2FAuth()
  54. {
  55. Google2FAuthDiv.Visible = true;
  56. verificationCode.Enabled = true;
  57. Google2FAuthDivCode.Visible = false;
  58. txtCompcode.Enabled = false;
  59. }
  60. private void DisableGoogle2FAuth()
  61. {
  62. Google2FAuthDiv.Visible = false;
  63. verificationCode.Enabled = false;
  64. Google2FAuthDivCode.Visible = true;
  65. txtCompcode.Enabled = true;
  66. }
  67. private void GetLogin()
  68. {
  69. Username = Request.Form["username"];
  70. pwd = Request.Form["password"];
  71. Usercode = Request.Form["companycode"];
  72. Authenticate();
  73. }
  74. private void EnableLogin()
  75. {
  76. btnLogin.Enabled = true;
  77. }
  78. private void DisableLogin()
  79. {
  80. btnLogin.Enabled = false;
  81. }
  82. private void Authenticate()
  83. {
  84. var dbResult = new DbResult();
  85. DisableLogin();
  86. //if (!GetStatic.IsNumeric(Usercode))
  87. //{
  88. // dbResult.ErrorCode = "1";
  89. // dbResult.Msg = "Login fails, Incorrect user name or password or user code !";
  90. // // jsonSerialize(dbResult);
  91. // errMsg.InnerText = dbResult.Msg;
  92. // errMsg.Visible = true;
  93. // EnableLogin();
  94. // return;
  95. //}
  96. var dr = user.DoLogin(Username, pwd, Usercode, ipAddress, GetUserInfo(), GetStatic.GetLocation(ipAddress), verificationCode.Text, GetStatic.ReadWebConfig("UseGoogle2FAuthAdmin", ""));
  97. if (null == dr)
  98. {
  99. var msg = "wrong credentials.";
  100. // GetStatic.CallBackJs1(this, "", "ShowErrorMsg('" + msg + "');");
  101. errMsg.InnerText = msg;
  102. errMsg.Visible = true;
  103. EnableLogin();
  104. return;
  105. }
  106. if (dr.ErrorCode != "0")
  107. {
  108. //jsonSerialize(dr);
  109. // GetStatic.CallBackJs1(this, "", "ShowErrorMsg('" + dr.Msg + "');");
  110. errMsg.InnerText = dr.Msg;
  111. errMsg.Visible = true;
  112. EnableLogin();
  113. return;
  114. }
  115. if (GetStatic.ReadWebConfig("UseGoogle2FAuthAdmin", "") == "Y")
  116. {
  117. if (string.IsNullOrEmpty(dr.UserUniqueKey))
  118. {
  119. errMsg.InnerText = "Please contact JME Head office to get QR code for accessing JME Remit system!";
  120. errMsg.Visible = true;
  121. EnableLogin();
  122. return;
  123. }
  124. var _dbRes = _auth.Verify2FA(verificationCode.Text, dr.UserUniqueKey);
  125. user.Log2FAuth(dr.logId, _dbRes.ErrorCode == "0" ? "1" : "0");
  126. if (_dbRes.ErrorCode != "0")
  127. {
  128. errMsg.InnerText = _dbRes.Msg;
  129. errMsg.Visible = true;
  130. EnableLogin();
  131. return;
  132. }
  133. }
  134. if (dr.UserAccessLevel.ToUpper() == "S")
  135. {
  136. var cookieKey = Username + "_userSessionId";
  137. var lastUserSessionId = GetStatic.ReadCookie(cookieKey, "");
  138. if (userPool.IsUserExists(Username))
  139. {
  140. if (!userPool.IsUserExists(Username, lastUserSessionId))
  141. {
  142. Session.Add("usr", Username);
  143. var url = GetStatic.GetUrlRoot() + "/LoginSession.aspx";
  144. Response.Redirect(url);
  145. return;
  146. }
  147. else
  148. {
  149. userPool.RemoveUser(Username);
  150. }
  151. }
  152. }
  153. var db = ManageUserSession(dr);
  154. if (db.ErrorCode != "0")
  155. {
  156. //jsonSerialize(dr);
  157. // GetStatic.CallBackJs1(this, "", "ShowErrorMsg('" + dr.Msg + "');");
  158. errMsg.InnerText = db.Msg;
  159. errMsg.Visible = true;
  160. EnableLogin();
  161. return;
  162. }
  163. //jsonSerialize(dr);
  164. //return;
  165. if (dr.isForcePwdChanged.ToUpper() == "Y")
  166. {
  167. Response.Redirect("../SwiftSystem/UserManagement/ApplicationUserSetup/ChangePassword.aspx");
  168. }
  169. else
  170. {
  171. //Get2FAuthentication();
  172. //Response.Redirect("/Admin/Dashboard.aspx");
  173. Response.Redirect("/DashboardV2/Dashboard.aspx");
  174. }
  175. EnableLogin();
  176. // Response.Redirect("Popup.aspx");
  177. }
  178. private void Get2FAuthentication()
  179. {
  180. GoogleAuthenticatorModel _model = new GoogleAuthenticatorModel();
  181. login.Visible = false;
  182. //authenticate.Visible = true;
  183. _model = _auth.GenerateCodeAndImageURL(Username);
  184. //imgVerifyQRCode.ImageUrl = _model.BarCodeImageUrl;
  185. }
  186. private DbResult ManageUserSession(UserDetails ud)
  187. {
  188. Session.Clear();
  189. var res = SetUserPool(ud);
  190. if (res.ErrorCode != "0")
  191. return res;
  192. GetStatic.WriteSession("admin", Username);
  193. GetStatic.WriteSession("fullname", ud.FullName);
  194. GetStatic.WriteSession("branchId", ud.Branch);
  195. GetStatic.WriteSession("branchName", ud.BranchName);
  196. GetStatic.WriteSession("address", ud.Address);
  197. GetStatic.WriteSession("userType", ud.UserType);
  198. GetStatic.WriteCookie("loginType", "ADMIN");
  199. Session[Username + "Menu"] = new StringBuilder();
  200. Session.Timeout = Convert.ToInt16(ud.sessionTimeOut);
  201. var cookieKey = Username + "_userSessionId";
  202. GetStatic.WriteCookie(cookieKey, GetStatic.GetSessionId());
  203. return res;
  204. }
  205. private DbResult SetUserPool(UserDetails ud)
  206. {
  207. HttpBrowserCapabilities browser = Request.Browser;
  208. var usr = new LoggedInUser();
  209. usr.UserId = GetStatic.ParseInt(ud.UserId);
  210. usr.UserName = Username;
  211. usr.UserFullName = ud.FullName;
  212. usr.LoginTime = DateTime.Now;
  213. usr.UserAccessLevel = ud.UserAccessLevel;
  214. usr.UserAgentName = ud.BranchName;
  215. usr.LastLoginTime = Convert.ToDateTime(ud.LastLoginTs);
  216. usr.LoggedInCountry = ud.LoggedInCountry;
  217. usr.LoginAddress = ud.LoginAddress;
  218. usr.LastLoginTime = Convert.ToDateTime(ud.LastLoginTs);
  219. usr.Browser = browser.Browser + "/" + browser.Type;
  220. usr.IPAddress = ipAddress;
  221. usr.SessionID = GetStatic.GetSessionId();
  222. usr.LastActiveTime = usr.LoginTime;
  223. return userPool.AddUser(usr);
  224. }
  225. private string GetUserInfo()
  226. {
  227. HttpBrowserCapabilities browser = Request.Browser;
  228. string str = " Browser Capabilities = Values -:::-"
  229. + "Type = " + browser.Type + "-:::-" //-:::-
  230. + "Name = " + browser.Browser + "-:::-"
  231. + "Version = " + browser.Version + "-:::-"
  232. + "Major Version = " + browser.MajorVersion + "-:::-"
  233. + "Minor Version = " + browser.MinorVersion + "-:::-"
  234. + "Platform = " + browser.Platform + "-:::-"
  235. + "Is Beta = " + browser.Beta + "-:::-"
  236. + "Is Crawler = " + browser.Crawler + "-:::-"
  237. + "Is AOL = " + browser.AOL + "-:::-"
  238. + "Is Win16 = " + browser.Win16 + "-:::-"
  239. + "Is Win32 = " + browser.Win32 + "-:::-"
  240. + "Supports Frames = " + browser.Frames + "-:::-"
  241. + "Supports Tables = " + browser.Tables + "-:::-"
  242. + "Supports Cookies = " + browser.Cookies + "-:::-"
  243. + "Supports VBScript = " + browser.VBScript + "-:::-"
  244. + "Supports JavaScript = " + browser.EcmaScriptVersion.ToString() + "-:::-"
  245. + "Supports Java Applets = " + browser.JavaApplets + "-:::-"
  246. + "Supports ActiveX Controls = " + browser.ActiveXControls + "-:::-"
  247. + "Supports JavaScript Version = " + browser["JavaScriptVersion"] + "-:::-"
  248. + "CDF = " + browser.CDF + "-:::-"
  249. + "IP Adress = " + ipAddress + "-:::-"
  250. //+ "Certificate ID No = " + dcIdNo + "-:::-"
  251. //+ "Certificate User Name = " + dcUserName + "-:::-"
  252. + "User Agent = " + Request.ServerVariables["HTTP_USER_AGENT"] + "-:::-"
  253. + "Refrerer = " + Request.ServerVariables["HTTP_REFERER"] + "-:::-"
  254. + "Http Accept = " + Request.ServerVariables["HTTP_ACCEPT"] + "-:::-"
  255. + "Language = " + Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];
  256. return str;
  257. }
  258. public void jsonSerialize<T>(T obk)
  259. {
  260. JavaScriptSerializer jsonData = new JavaScriptSerializer();
  261. string jsonString = jsonData.Serialize(obk);
  262. Response.ContentType = "application/json";
  263. Response.Write(jsonString);
  264. Response.End();
  265. }
  266. private void GetServerCredentials()
  267. {
  268. Username = txtUsername.Text;
  269. pwd = txtPwd.Text;
  270. Usercode = txtCompcode.Text;
  271. Authenticate();
  272. }
  273. protected void btnLogin_Click(object sender, EventArgs e)
  274. {
  275. DisableLogin();
  276. GetServerCredentials();
  277. }
  278. //protected void bntSubmit_Click(object sender, EventArgs e)
  279. //{
  280. // //Authenticate();
  281. //}
  282. }
  283. }