// [ValidateAntiForgeryToken] public async Task <IActionResult> Edit(DomainSettingViewModel model) { try { model.SystemUserId = UserId; if (ModelState.IsValid) { var item = _mapper.Map <DomainSettingViewModel, DomainSetting>(model); var result = await _domainRepository.UpdateAsync(item); ErrorMessage = Resources.Messages.ChangesSavedSuccessfully; if (Request.Form.Keys.Contains("SaveAndReturn")) { return(RedirectToAction("Index")); } else { return(RedirectToAction("Edit", new { id = item.Id })); } } return(View(model)); } catch (Exception e) { Log.Error(e, e.Message); return(View("~/Views/Shared/Error.cshtml", new ErrorViewModel { RequestId = e.Message })); } }
//[Authorize(Roles = "Admin")] public async Task <IActionResult> Create(DomainSettingViewModel model) { try { model.SystemUserId = UserId; if (ModelState.IsValid) { string clearText = ""; var item = _mapper.Map <DomainSettingViewModel, DomainSetting>(model); clearText = _encriptdescriptStringRepository.EncryptString(model.Password); item.Password = clearText; await _domainRepository.InsertAsync(item); ErrorMessage = Resources.Messages.ChangesSavedSuccessfully; if (Request.Form.Keys.Contains("SaveAndReturn")) { return(RedirectToAction("Index")); } else { return(RedirectToAction("Edit", new { id = item.Id })); } } return(View(model)); } catch (Exception e) { Log.Error(e, e.Message); return(View("~/Views/Shared/Error.cshtml", new ErrorViewModel { RequestId = e.Message })); } }
private List <DomainUserViewModel> FindAllADUsers(int id, string userName)// GetAllADUsers(int id, string UseName) { try { string cipherText = ""; IQueryable <DomainSetting> domainSetting = _context.DomainSetting.Where(w => w.Id == id); var domain = domainSetting.Select(w => new DomainSetting { UserName = w.UserName, Server = w.Server, Title = w.Title, Password = w.Password }).FirstOrDefault(); string EncryptionKey = "MAKV2SPBNI99212"; byte[] cipherBytes = Convert.FromBase64String(domain.Password); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(cipherBytes, 0, cipherBytes.Length); cs.Close(); } cipherText = Encoding.Unicode.GetString(ms.ToArray()); } } List <DomainUserViewModel> lstADUsers = new List <DomainUserViewModel>(); DomainUserViewModel objSurveyUsers = new DomainUserViewModel(); DomainSettingViewModel SelectAll = new DomainSettingViewModel(); string dcString = ""; string rootNode = ""; string[] arrString; arrString = domain.Title.Split('.'); if (arrString.Length == 1) { dcString = "dc=" + domain.Title + ""; rootNode = arrString[0]; } else { for (int i = 0; i != arrString.Length; i++) { dcString += "dc=" + arrString[i].ToString() + ","; } if (arrString.Length == 3) { rootNode = arrString[1].ToString(); } else if (arrString.Length == 2) { rootNode = arrString[0].ToString(); } dcString = dcString.Substring(0, dcString.Length - 1); } try { string DomainPath = "LDAP://" + domain.Server + "/" + dcString; System.DirectoryServices.DirectoryEntry searchRoot = new System.DirectoryServices.DirectoryEntry(DomainPath); searchRoot.Username = domain.UserName; searchRoot.Password = cipherText; DirectorySearcher search = new DirectorySearcher(searchRoot); if (userName == "*") { search.Filter = $"(objectClass=user)"; } else { userName = userName.Split("@")[0]; search.Filter = $"(samaccountname=*{userName}*)"; } search.PropertiesToLoad.Add("samaccountname"); search.PropertiesToLoad.Add("mail"); search.PropertiesToLoad.Add("usergroup"); search.PropertiesToLoad.Add("displayname"); //first name search.PropertiesToLoad.Add("givenname"); //first name search.PropertiesToLoad.Add("sn"); //first name SearchResult resultFetch; SearchResultCollection resultCol = search.FindAll(); if (resultCol != null) { for (int counter = 0; counter < resultCol.Count; counter++) { string UserNameEmailString = string.Empty; resultFetch = resultCol[counter]; if (resultFetch.Properties.Contains("samaccountname")) { objSurveyUsers = new DomainUserViewModel(); if (resultFetch.Properties.Contains("mail")) { objSurveyUsers.Email = (String)resultFetch.Properties["mail"][0]; } else { // objSurveyUsers.Email = (String)resultFetch.Properties["samaccountname"][0] + id.ToString() + "@Pointer.com"; } if (resultFetch.Properties.Contains("displayname")) { objSurveyUsers.DisplayName = (String)resultFetch.Properties["displayname"][0]; } else { objSurveyUsers.DisplayName = (String)resultFetch.Properties["samaccountname"][0]; } objSurveyUsers.UserName = (String)resultFetch.Properties["samaccountname"][0]; if (resultFetch.Properties.Contains("givenname")) { objSurveyUsers.FirstName = (String)resultFetch.Properties["givenname"][0]; } else { objSurveyUsers.FirstName = (String)resultFetch.Properties["samaccountname"][0]; } if (resultFetch.Properties.Contains("sn")) { objSurveyUsers.LastName = (String)resultFetch.Properties["sn"][0]; } else { objSurveyUsers.LastName = (String)resultFetch.Properties["samaccountname"][0]; } objSurveyUsers.dcString = dcString; lstADUsers.Add(objSurveyUsers); } } } } catch (Exception ex) { } return(lstADUsers); } catch (Exception e) { Log.Error(e, e.Message); return(null); } }
public IActionResult Index() { try { int DomainId = 0; string Password = ""; // var items = _domainRepository.GetAsQueryable(x => HasAccess1(x.Id)).ToList(); var items = _domainRepository.GetAll().ToList(); var model = _mapper.Map <IEnumerable <DomainSetting>, IEnumerable <DomainSettingViewModel> >(items); DomainSettingViewModel m = new DomainSettingViewModel(); foreach (var x in model) { m.Id = x.Id; m.Server = x.Server; m.Title = x.Title; m.IsActive = x.IsActive; m.Password = x.Password; } IQueryable <DomainSetting> domainSetting = _context.DomainSetting.Where(w => w.Title != "کاربران سیستمی"); var domain = domainSetting.Select(w => new DomainSetting { Id = w.Id, Server = w.Server, Title = w.Title, UserName = w.UserName, Password = w.Password }).FirstOrDefault(); if (domain != null) { string cipherText = ""; cipherText = _encriptdescriptStringRepository.DecryptString(domain.Password); DomainId = domain.Id; Password = cipherText; bool ping = _pingLdap.Ping(DomainId, Password); if (ping) { if (m.Title != "کاربران سیستمی") { m.IsActive = true; } foreach (var y in model) { if (y.Title != "کاربران سیستمی") { y.IsActive = m.IsActive; } } } return(View(model)); } else { return(View(model)); } } catch (Exception e) { Log.Error(e, e.Message); return(View("~/Views/Shared/Error.cshtml", new ErrorViewModel { RequestId = e.Message })); } }