private void newMapButton_Click(object sender, EventArgs e) { DriveMap map = new DriveMap(); map.Drive = m_driveOptions[0]; map.Credentials = DriveMap.CredentialOption.Final; // Choose a drive letter that's available HashSet <string> usedLetters = new HashSet <string>(); foreach (DriveMap m in m_maps) { usedLetters.Add(m.Drive); } for (int i = 0; i < m_driveOptions.Length; i++) { if (!usedLetters.Contains(m_driveOptions[i])) { map.Drive = m_driveOptions[i]; break; } } m_maps.Add(map); m_mapListDgv.Rows[m_mapListDgv.Rows.Count - 1].Selected = true; }
private void SetEditFields(DriveMap map) { m_driveLetterCb.SelectedItem = map.Drive; m_uncPathTb.Text = map.UncPath; m_credsCb.SelectedItem = map.Credentials.ToString(); m_usernameTb.Text = map.Username; m_passwordTb.Text = map.Password; m_groupTb.Text = map.Group; }
private void m_driveLetterCb_SelectedIndexChanged(object sender, EventArgs e) { if (m_mapListDgv.SelectedRows.Count > 0) { DataGridViewRow row = m_mapListDgv.SelectedRows[0]; int idx = row.Index; DriveMap map = (DriveMap)row.DataBoundItem; map.Drive = m_driveLetterCb.SelectedItem.ToString(); m_maps.ResetItem(idx); } }
void m_uncPathTb_LostFocus(object sender, EventArgs e) { if (m_mapListDgv.SelectedRows.Count > 0) { DataGridViewRow row = m_mapListDgv.SelectedRows[0]; int idx = row.Index; DriveMap map = (DriveMap)row.DataBoundItem; map.UncPath = m_uncPathTb.Text; m_maps.ResetItem(idx); } }
private void m_credsCb_SelectedIndexChanged(object sender, EventArgs e) { if (m_mapListDgv.SelectedRows.Count > 0) { DataGridViewRow row = m_mapListDgv.SelectedRows[0]; int idx = row.Index; DriveMap map = (DriveMap)row.DataBoundItem; map.Credentials = (DriveMap.CredentialOption)Enum.Parse(typeof(DriveMap.CredentialOption), m_credsCb.SelectedItem.ToString()); m_maps.ResetItem(idx); if (map.Credentials == DriveMap.CredentialOption.Custom) { EnableCredentialsFields(); } else { DisableAndClearCredentialsFields(); } } }
void m_mapListDgv_SelectionChanged(object sender, EventArgs e) { int numRowsSelected = m_mapListDgv.SelectedRows.Count; if (numRowsSelected == 0) { DisableAndClearAllEditFields(); } else { DataGridViewRow row = m_mapListDgv.SelectedRows[0]; DriveMap map = (DriveMap)row.DataBoundItem; SetEditFields(map); EnableEditFields(); if (map.Credentials == DriveMap.CredentialOption.Custom) { EnableCredentialsFields(); } } }
private void MapDrive(int sessId, DriveMap map, SessionProperties sessProperties) { // Get user information UserInformation userInfo = sessProperties.GetTrackedSingle <UserInformation>(); // Update the UNC path if there are any place-holders such as (%u or %o) map.UncPath = map.UncPath.Replace("%u", userInfo.Username); map.UncPath = map.UncPath.Replace("%o", userInfo.OriginalUsername); // Check that we're in the right group for this share if (!string.IsNullOrEmpty(map.Group)) { bool ok = false; foreach (GroupInformation gi in userInfo.Groups) { if (gi.Name.Equals(map.Group, StringComparison.CurrentCultureIgnoreCase)) { ok = true; break; } } if (!ok) { m_logger.DebugFormat("User is not in group {0}, skipping drive map: {1}", map.Group, map.UncPath); return; } } // Set the credentials if necessary if (map.Credentials == DriveMap.CredentialOption.Final) { map.Username = userInfo.Username; map.Password = userInfo.Password; } else if (map.Credentials == DriveMap.CredentialOption.Original) { map.Username = userInfo.OriginalUsername; map.Password = userInfo.OriginalPassword; } m_logger.InfoFormat("Mapping '{0}' with username '{1}' to drive '{2}'", map.UncPath, map.Username, map.Drive); // Map the drive in another thread, because the thread will do user impersonation // and we'd rather not do that in the service's main thread. Thread mapThread = new Thread(delegate() { try { pInvokes.MapDriveInSession(sessId, map.UncPath, map.Drive, map.Username, map.Password); } catch (Win32Exception ex) { m_logger.ErrorFormat("Failed to map drive {0}: {1}", map.UncPath, ex.Message); m_logger.ErrorFormat(" Win32 error code: {0}", ((Win32Exception)ex).NativeErrorCode); } catch (Exception ex) { m_logger.ErrorFormat("Failed to map drive, unexpected exception: {0}", ex); } }); mapThread.Start(); mapThread.Join(); }
private void newMapButton_Click(object sender, EventArgs e) { DriveMap map = new DriveMap(); map.Drive = m_driveOptions[0]; map.Credentials = DriveMap.CredentialOption.Final; // Choose a drive letter that's available HashSet<string> usedLetters = new HashSet<string>(); foreach (DriveMap m in m_maps) { usedLetters.Add(m.Drive); } for (int i = 0; i < m_driveOptions.Length; i++) { if(!usedLetters.Contains(m_driveOptions[i])) { map.Drive = m_driveOptions[i]; break; } } m_maps.Add(map); m_mapListDgv.Rows[m_mapListDgv.Rows.Count - 1].Selected = true; }
private void MapDrive(int sessId, DriveMap map, SessionProperties sessProperties) { // Get user information UserInformation userInfo = sessProperties.GetTrackedSingle<UserInformation>(); // Update the UNC path if there are any place-holders such as (%u or %o) map.UncPath = map.UncPath.Replace("%u", userInfo.Username); map.UncPath = map.UncPath.Replace("%o", userInfo.OriginalUsername); // Check that we're in the right group for this share if( ! string.IsNullOrEmpty(map.Group) ) { bool ok = false; foreach(GroupInformation gi in userInfo.Groups ) { if( gi.Name.Equals(map.Group, StringComparison.CurrentCultureIgnoreCase) ) { ok = true; break; } } if (!ok) { m_logger.DebugFormat("User is not in group {0}, skipping drive map: {1}", map.Group, map.UncPath); return; } } // Set the credentials if necessary if (map.Credentials == DriveMap.CredentialOption.Final) { map.Username = userInfo.Username; map.Password = userInfo.Password; } else if( map.Credentials == DriveMap.CredentialOption.Original ) { map.Username = userInfo.OriginalUsername; map.Password = userInfo.OriginalPassword; } m_logger.InfoFormat("Mapping '{0}' with username '{1}' to drive '{2}'", map.UncPath, map.Username, map.Drive); // Map the drive in another thread, because the thread will do user impersonation // and we'd rather not do that in the service's main thread. Thread mapThread = new Thread(delegate() { try { pInvokes.MapDriveInSession(sessId, map.UncPath, map.Drive, map.Username, map.Password); } catch (Win32Exception ex) { m_logger.ErrorFormat("Failed to map drive {0}: {1}", map.UncPath, ex.Message); m_logger.ErrorFormat(" Win32 error code: {0}", ((Win32Exception)ex).NativeErrorCode); } catch(Exception ex) { m_logger.ErrorFormat("Failed to map drive, unexpected exception: {0}", ex); } } ); mapThread.Start(); mapThread.Join(); }