public ActionResult ImportUsers(int serverlogin, string conn, HttpPostedFileBase file) { byte result = 1; string msg = null; Person p = null; Userdb udb = null; if (serverlogin == 1) { udb = new Userdb(); udb.Auth = 1; udb.Conn = conn; } long total = 0; try { int n = 0; string fio = null; AccessMembershipProvider ap = (AccessMembershipProvider)Membership.Provider; XDocument xd = XDocument.Load(file.InputStream); IEnumerable <XElement> elements = xd.Descendants("Users"); total = elements.Count(); ThreadContext.Properties["user"] = User.Identity.Name; foreach (var u in elements) { p = new Person(); p.Bases = new List <Userdb>(); p.Roles = new List <string>() { "READER" }; p.IsApproved = 1; p.Login = u.Element("Name").Value; System.Diagnostics.Debug.WriteLine(p.Login); fio = u.Element("FullName").Value; if (!string.IsNullOrEmpty(fio)) { int pos = fio.IndexOf(' '); if (pos != -1) { p.LastName = fio.Substring(0, pos++); string[] arr = fio.Substring(pos).Split(new char[] { '.', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (arr.Length > 0) { p.FirstName = arr[0]; if (arr.Length > 1) { p.MiddleName = arr[1]; } } } else { p.LastName = p.FirstName = fio; } } if (serverlogin == 1) { p.ServerLogin = 1; p.Bases.Add(udb); } else { p.Password = "******"; } p.Id = db.SingleOrDefault <int>("SELECT u.id FROM qb_users u WHERE u.login = @0", p.Login); try { if (p.Id != 0) { Membership.Provider.UpdateUser(p.Id, p); } else { Membership.Provider.CreateUserAndAccount(p); } n++; System.Diagnostics.Debug.WriteLine(n); #if DEBUG if (n == 2) { break; } #endif } catch (MembershipCreateUserException e) { log.Info("Импорт " + p.Login + ". " + e.Message); } } msg = n.ToString(); Response.RemoveOutputCacheItem("/Admin/GetUser"); Response.RemoveOutputCacheItem("/Admin/GetUsers"); } catch (Exception e) { msg = e.Message; result = 0; } string json = Newtonsoft.Json.JsonConvert.SerializeObject(new { success = result, message = msg, total = total }); return(Content(json)); }
public JsonNetResult UpdateSettings(SettingsModel sm) { byte result = 1; string msg = null; try { Configuration cfg = WebConfigurationManager.OpenWebConfiguration("~"); AppSettingsSection ass = cfg.AppSettings; string key = "ConnTimeout", val = sm.ConnTimeout.ToString(); if (ass.Settings[key] != null) { ass.Settings[key].Value = val; } else { ass.Settings.Add(key, val); } key = "ItemsPerPage"; val = sm.ItemsPerPage.ToString(); if (ass.Settings[key] != null) { ass.Settings[key].Value = val; } else { ass.Settings.Add(key, val); } if (ass.Settings[key] != null) { ass.Settings[key].Value = val; } else { ass.Settings.Add(key, val); } MembershipSection ms = (MembershipSection)cfg.GetSection("system.web/membership"); ProviderSettings ps = ms.Providers["AccessMembershipProvider"]; key = "maxInvalidPasswordAttempts"; val = sm.MaxInvalidPasswordAttempts.ToString(); ps.Parameters[key] = val; key = "minRequiredPasswordLength"; val = sm.MinRequiredPasswordLength.ToString(); ps.Parameters[key] = val; key = "minRequiredUsernameLength"; val = sm.MinRequiredUsernameLength.ToString(); ps.Parameters[key] = val; key = "passwordAnswerAttemptLockoutDuration"; val = sm.PasswordAnswerAttemptLockoutDuration.ToString(); ps.Parameters[key] = val; key = "saltLength"; val = sm.SaltLength.ToString(); ps.Parameters[key] = val; // update provider AccessMembershipProvider provider = (AccessMembershipProvider)Membership.Provider; provider.InitConfig(ps.Parameters as NameValueCollection); cfg.Save(ConfigurationSaveMode.Modified); Response.RemoveOutputCacheItem("/Admin/GetSettings"); } catch (Exception e) { msg = e.Message; result = 0; } JsonNetResult jr = new JsonNetResult(); jr.Data = new { success = result, message = msg }; return(jr); }