本文整理汇总了C#中System.DirectoryServices.DirectoryEntry.MoveTo方法的典型用法代码示例。如果您正苦于以下问题:C# DirectoryEntry.MoveTo方法的具体用法?C# DirectoryEntry.MoveTo怎么用?C# DirectoryEntry.MoveTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.DirectoryServices.DirectoryEntry
的用法示例。
在下文中一共展示了DirectoryEntry.MoveTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MoveADObjectIfNecessary
private void MoveADObjectIfNecessary(DirectoryEntry entry, SyncTree entrySyncTree, Node node, string newPath)
{
if (!_config.AllowMove)
return;
// ebben a synctree-ben van
var oldSyncTree = entrySyncTree;
// ebben a synctree-ben kell lennie
var newSyncTree = GetSyncTreeContainingPortalPath(newPath);
// itt már csak azonos szerveren történő moveolás lehetséges (különböző szerverre történő moveolás feljebb történik)
// a sync tartományok esetleg különbözhetnek
var assumedPortalParentPath = oldSyncTree.GetPortalParentPath(entry.Path);
var portalParentPath = RepositoryPath.GetParentPath(newPath);
// változott a path, moveolni kell
if (assumedPortalParentPath != portalParentPath)
{
var parentEntryName = newSyncTree.GetADPath(portalParentPath);
using (DirectoryEntry parentEntry = newSyncTree.ConnectToObject(parentEntryName))
{
entry.MoveTo(parentEntry);
}
}
}
示例2: MoveItemToNewPath
private static void MoveItemToNewPath(IOguObject oguObject, DirectoryEntry targetObject)
{
ADHelper adHelper = SynchronizeContext.Current.ADHelper;
IDMapping mapping = null;
var oguParent = oguObject.Parent;
if (oguParent != null)
{
mapping = SynchronizeContext.Current.IDMapper.GetAdObjectMapping(oguParent.ID, false);
}
if (mapping != null)
{
using (var adEntry = SynchronizeHelper.GetDirectoryEntryByID(SynchronizeContext.Current.ADHelper, mapping.ADObjectGuid))
{
if (adEntry != null)
{
try
{
targetObject.MoveTo(adEntry);
}
catch (DirectoryServicesCOMException comEx)
{
switch (comEx.ErrorCode)
{
case -2147019886:
//重名
SynchronizeHelper.SolveConflictAboutMove(oguObject, targetObject, adEntry);
break;
case -2147016656:
Trace.WriteLine("修改对象属性后,移动对象说对象不存在??被移动的对象:" + oguObject.Name);
throw;
default:
throw;
}
}
}
}
}
else
{
var parentDn = SynchronizeHelper.GetParentObjectDN(oguObject);
Trace.WriteLine("不可靠的方法被执行,目标不存在时,靠DN来延迟移动。Careful !");
// 父对象还不存在,这可能是被重命名了
//SynchronizeContext.Current.DelayActions.Add(new DelayMoveToAction(oguObject, parentDn, targetObject.Guid));
// 没有这种可能性
var context = SynchronizeContext.Current;
context.ExceptionCount++;
LogHelper.WriteSynchronizeDBLogDetail(SynchronizeContext.Current.SynchronizeID, "移动对象", oguObject.ID, oguObject.Name,
context.ADHelper.GetPropertyStrValue("objectGuid", targetObject),
context.ADHelper.GetPropertyStrValue("distinguishedName", targetObject),
string.Format("父对象:{0}无映射,无法移动。", parentDn));
}
}
示例3: DoTheMove
private static void DoTheMove(DirectoryEntry targetObject, DirectoryEntry parentEntry, IOguObject oguObj)
{
bool fail = false;
string ldif;
StringBuilder builder = GetEntryLongNameBuilder(targetObject, out ldif);
do
{
try
{
string srcDN;
try
{
srcDN = targetObject.Properties["distinguishedName"].Value as string;
}
catch
{
srcDN = "无法获取起始路径";
}
targetObject.MoveTo(parentEntry, ldif + "=" + ADHelper.EscapeString(builder.ToString()));
targetObject.CommitChanges();
try
{
LogHelper.WriteSynchronizeDBLogDetail(SynchronizeContext.Current.SynchronizeID, "应用变更: 移到回收站 ",
oguObj == null ? string.Empty : oguObj.ID,
oguObj == null ? string.Empty : oguObj.Name,
targetObject == null ? string.Empty : targetObject.NativeGuid.ToString(),
targetObject == null ? string.Empty : targetObject.Name, "删除动作,对象的初始可分辨名称为:" + srcDN);
}
catch (Exception)
{
}
fail = false;
}
catch (DirectoryServicesCOMException comEx)
{
switch (comEx.ErrorCode)
{
case -2147019886: // 重名
MakeTimePostfixToBuilderAndTrim(builder);
fail = true;
Thread.Sleep(100);
break;
default:
throw;
}
}
} while (fail);
}
示例4: MoveObject
/// <summary>
/// move an obect in active directory
/// </summary>
/// <param name="objectDN"></param>
/// <param name="targetDN"></param>
/// <returns></returns>
public bool MoveObject(string objectDN, string targetDN)
{
try
{
DirectoryEntry eLocation = new DirectoryEntry("LDAP://" + objectDN);
DirectoryEntry nLocation = new DirectoryEntry("LDAP://" + targetDN);
string newName = eLocation.Name;
eLocation.MoveTo(nLocation, newName);
nLocation.Close();
eLocation.Close();
return true;
}
catch (Exception ex)
{
Mailer.ErrNotify(ex, "ActiveDirectory MoveObject");
return false;
}
}
示例5: RemoveGroups
/// <summary>
/// Expires, disables, removes AD groups, and moves AD account to disabled OU.
/// </summary>
/// <param name="userName">active directory user id</param>
public void RemoveGroups(string userName)
{
using (var context = new PrincipalContext(ContextType.Domain, Form1._Domain, Form1._AdminUser, Form1._Password))
{
// Expire account on prior day
ExpireAccount();
// Disable account
UserPrincipal user = UserPrincipal.FindByIdentity(context, userName);
user.Enabled = false;
string ldap = user.DistinguishedName;
user.Save();
user.Dispose();
// Move to disabled accounts ou
DirectoryEntry oUser = new DirectoryEntry("LDAP://" + ldap,Form1._AdminUser,Form1._Password);
DirectoryEntry nUser = new DirectoryEntry("LDAP://" + ConfigurationManager.AppSettings["LC1DisableOU"], Form1._AdminUser, Form1._Password);
oUser.MoveTo(nUser);
oUser.Dispose();
nUser.Dispose();
try
{
// Remove groups
foreach (var listItems in Form1.myForm.ListViewAD.Items)
{
GroupPrincipal group = GroupPrincipal.FindByIdentity(context, listItems.ToString());
if (group.Name != "Domain Users")
{
group.Members.Remove(UserPrincipal.FindByIdentity(context, userName));
group.Save();
Form1.myForm._Notes.AppendLine();
Form1.myForm._Notes.Append("<br>" + group.Name + " removed.");
group.Dispose();
}
}
Form1.myForm.cbRemoveAD.Checked = true;
Form1.myForm._Notes.AppendLine();
Form1.myForm._Notes.Append("<br>AD groups removed, account expired, account disabled, account moved to Disabled Accounts OU.<br>");
}
catch (Exception ex)
{
Form1.myForm.cbRemoveAD.Checked = false;
Form1.myForm._Notes.AppendLine();
Form1.myForm._Notes.Append("<br><b>Error in AD removal process.</b><br>" + ex.Message + "<br>");
}
finally
{
// delete user with CIM at end of name
try
{
UserPrincipal user2 = UserPrincipal.FindByIdentity(context, userName + "cim");
user2.Delete();
Form1.myForm._Notes.AppendLine();
Form1.myForm._Notes.Append("<br>CIM Account Deleted.<br>");
}
catch (Exception ex)
{
Form1.myForm._Notes.AppendLine();
Form1.myForm._Notes.Append("<br>CIM Account failed to remove or does not have one.<br>" + ex.Message + "<br>");
}
}
}
}
示例6: CreateGroupToUnit
/// <summary>
/// 添加新组到指定的组织单元
/// </summary>
/// <param name="cn">用户组</param>
/// <param name="ouPath">组织单元路径(格式:OU=sddsd,OU=sdsdsd,顺序,子倒父)</param>
/// <param name="description">描述</param>
/// <returns>bool</returns>
public int CreateGroupToUnit(string cn,string description,string path,out string errStr)
{
int result=0;
errStr="";
//创建指定路径的组织单元对象
int i=0;int j=0;
//string LDAPDomain ="/"+ouPath.ToString()+Iadc.GetLDAPDomain() ;
//string LDAPDomain ="/"+ouPath.ToString()+ Iadc.GetLDAPDomain() ;
//DirectoryEntry oDE= Iadc.GetDirectoryObject(LDAPDomain);
DirectoryEntry oDE= Iadc.GetDirectoryObject(Iads.GetUnit(cn).ToString(),i,j);
//DirectoryEntry oDE= Iadc.GetDirectoryObject(ouPath);
DirectoryEntry oDEC=new DirectoryEntry();
try
{
if(!Iadch.CheckGroup(cn))
{
oDEC=oDE.Children.Add("cn="+cn,"group");
//oDEC.Properties["grouptype"].Value = ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP | ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED ;
oDEC.Properties["sAMAccountName"].Value = cn;
oDEC.Properties["description"].Value=description;
oDEC.Properties["displayName"].Value = path;
oDEC.CommitChanges();
result=1;
}
else
{
//移动组到正确的OU中
oDEC = Iads.GetGroupEntry(cn);
oDEC.Properties["displayName"].Value = path;
oDEC.CommitChanges();
oDEC.MoveTo(oDE);
oDE.CommitChanges();
result=2;
errStr="目录已存在该组,不能重复添加";
}
}
catch(Exception err)
{
result=0;
errStr=err.ToString();
}
finally
{
oDE.Close();
oDEC.Close();
}
return result;
}
示例7: MoveGroupToUnit2
/// <summary>
/// 将组移动到组织单元上
/// </summary>
/// <param name="cn"></param>
/// <param name="parentcn"></param>
public int MoveGroupToUnit2(string cn,string ou,out string errStr)
{
int result=0;
errStr="";
// string LDAPDomain ="/"+ouPath.ToString()+ Iadc.GetLDAPDomain() ;
// LDAPDomain=ouPath;
DirectoryEntry oDE=new DirectoryEntry();
DirectoryEntry oDEC=new DirectoryEntry();
if(!Iadch.CheckGroup(cn))return 2;
try
{
oDE=Iadc.GetDirectoryObject(Iads.GetUnit(ou),1,2);
oDEC=Iads.GetGroupEntry(cn);
oDEC.MoveTo(oDE);
oDE.CommitChanges();
result=1;
}
catch(Exception err)
{
result=0;
errStr=err.ToString();
}
finally
{
oDEC.Close();
oDE.Close();
}
return result;
}
示例8: btnPremesti_Click
private void btnPremesti_Click(object sender, EventArgs e)
{
if (cmbOU.SelectedItem != null)
{
string userOU = "LDAP://" + lblDC.Text + @"/" + cmbOU.SelectedItem.ToString();
string moveUser = lblDName.Text.ToString();
DirectoryEntry de = new DirectoryEntry("LDAP://" + lblDomain.Text);
de.Username = lblAdminUser.Text;
de.Password = lblPwd.Text;
DirectorySearcher ds = new DirectorySearcher(de);
ds.SearchScope = SearchScope.Subtree;
ds.Filter = "(&(objectCategory=User) (samAccountName=" + "*" + lblDName.Text + "*" + "))";
ds.PropertiesToLoad.Clear();
ds.PropertiesToLoad.Add("distinguishedName");
try
{
SearchResult result = ds.FindOne();
if (result != null)
{
string t = "LDAP://" + lblDC.Text + @"/" + result.Properties["distinguishedName"][0].ToString();
DirectoryEntry moved = new DirectoryEntry(t);
//MessageBox.Show(t);
//MessageBox.Show(userOU);
moved.MoveTo(new DirectoryEntry(userOU, lblAdminUser.Text, lblPwd.Text, AuthenticationTypes.Secure));
moved.CommitChanges();
moved.Close();
MessageBox.Show("USER ACCOUNT has been moved successfully!" + Environment.NewLine + result.Properties["distinguishedName"][0].ToString());
this.Close();
}
else
{
MessageBox.Show("Error, operation was not performed successfully!");
}
}
catch (Exception f)
{
MessageBox.Show(f.ToString());
}
}
}
示例9: fixUserOu
protected bool fixUserOu(String adpath)
{
DirectoryEntry de = new DirectoryEntry(adpath);
if (userIsInRightOU(de)) { return false; }
//See if it can be fixed!
String dn = (string)de.Properties["distinguishedName"][0];
String[] dnarray = dn.Split(',');
String[] ou = dnarray.Where(x => x.StartsWith("ou=", StringComparison.CurrentCultureIgnoreCase)).ToArray<String>();
int count = ou.Count();
if (count < 2)
{
//This cant be in people/{staff,student,guest}
return false;
}
//Check root is people
if (!(ou[count - 1]).Equals("ou=people", StringComparison.CurrentCultureIgnoreCase))
{
//Error user is not placed in people!!!!! Cant move the user (might not be a real user or admin or computer)
return false;
}
String[] okplaces = new String[3] { "ou=staff", "ou=guests", "ou=students" };
if (!okplaces.Contains(ou[count - 2], StringComparer.OrdinalIgnoreCase))
{
//Error user is not in out staff, people or student, what is gowing on here?
return false;
}
if (count > 2)
{
//User is not placed in people/{staff,student,guest}, but in a sub ou, we need to do somthing!
//from above check we know the path is people/{staff,student,guest}, lets generate new OU
//Format ldap://DOMAIN/pathtoOU
//return false; //XX Return false here?
String[] adpathsplit = adpath.ToLower().Replace("ldap://", "").Split('/');
String protocol = "LDAP://";
String domain = adpathsplit[0];
String[] dcpath = (adpathsplit[0].Split(',')).Where<string>(s => s.StartsWith("dc=", StringComparison.CurrentCultureIgnoreCase)).ToArray<String>();
String newOU = String.Format("{0},{1}", ou[count - 2], ou[count - 1]);
String newPath = String.Format("{0}{1}/{2},{3}", protocol, String.Join(".", dcpath).Replace("dc=", ""), newOU, String.Join(",", dcpath));
logger.Info("user " + System.Web.HttpContext.Current.User.Identity.Name + " changed OU on user to: " + newPath + " from " + adpath + ".");
var newLocaltion = new DirectoryEntry(newPath);
de.MoveTo(newLocaltion);
return true;
}
//We don't need to do anything, user is placed in the right ou! (we think, can still be in wrong ou fx a guest changed to staff, we cant check that here)
logger.Debug("no need to change user {0} out, all is good", adpath);
return true;
}
示例10: DirectoryEntry_MoveTo_DeStr
public void DirectoryEntry_MoveTo_DeStr()
{
string barakTsabariHumanResourcesDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
string barakTsabariDevQaDN = configuration.ServerRoot + "cn=My Name,ou=DevQA,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
using (DirectoryEntry barakTsabariDE = new DirectoryEntry( barakTsabariHumanResourcesDN,
configuration.Username,
configuration.Password,
configuration.AuthenticationType)){
string devQaOU = configuration.ServerRoot + "ou=DevQA,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
using (DirectoryEntry devQaDE = new DirectoryEntry(devQaOU,
configuration.Username,
configuration.Password,
configuration.AuthenticationType)){
barakTsabariDE.MoveTo(devQaDE,"cn=My Name");
barakTsabariDE.CommitChanges();
Assert.IsTrue(DirectoryEntry.Exists(barakTsabariDevQaDN));
string humanRwsourcesOU = configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
using (DirectoryEntry humanResourcesDE = new DirectoryEntry( humanRwsourcesOU,
configuration.Username,
configuration.Password,
configuration.AuthenticationType)){
barakTsabariDE.MoveTo(humanResourcesDE,"cn=Barak Tsabari");
barakTsabariDE.CommitChanges();
Assert.IsTrue(DirectoryEntry.Exists(barakTsabariHumanResourcesDN));
}
}
}
}
示例11: HandleContainerChange
/// <summary>
/// This does not work ... for now, don't handle container changes
/// </summary>
/// <param name="type"></param>
/// <param name="directoryEntry"></param>
/// <param name="attributes"></param>
/// <param name="config"></param>
private static void HandleContainerChange(UpdateType type,
DirectoryEntry directoryEntry, ICollection<ConnectorAttribute> attributes,
ActiveDirectoryConfiguration config)
{
Name nameAttribute = ConnectorAttributeUtil.GetNameFromAttributes(attributes);
if(nameAttribute == null)
{
// no name, so must not be a container change
return;
}
if (!type.Equals(UpdateType.REPLACE))
{
// this only make sense for replace. you can't
// add a name or delete a name
return;
}
String oldContainer = GetParentDn(directoryEntry.Path);
String newContainer = GetParentDn(nameAttribute.GetNameValue());
if (!NormalizeLdapString(oldContainer).Equals(NormalizeLdapString(newContainer), StringComparison.OrdinalIgnoreCase))
{
if (newContainer != null)
{
try
{
if (!NormalizeLdapString(oldContainer).Equals(
NormalizeLdapString(newContainer), StringComparison.OrdinalIgnoreCase))
{
String newContainerLdapPath = ActiveDirectoryUtils.GetLDAPPath(
config.LDAPHostName, newContainer);
DirectoryEntry newContainerDe = new DirectoryEntry(newContainerLdapPath,
config.DirectoryAdminName, config.DirectoryAdminPassword);
directoryEntry.MoveTo(newContainerDe);
newContainerDe.Dispose();
}
}
catch (Exception e)
{
throw e;
}
}
}
}
示例12: Move
/// <summary>
/// Move an Object from one Location to Another
/// </summary>
/// <param name="objectLocation"></param>
/// <param name="newLocation"></param>
public void Move(string objectLocation, string newLocation)
{
DirectoryEntry eLocation = new DirectoryEntry("LDAP://" + objectLocation);
DirectoryEntry nLocation = new DirectoryEntry("LDAP://" + newLocation);
string newName = eLocation.Name;
eLocation.MoveTo(nLocation, newName);
nLocation.Close();
eLocation.Close();
}
示例13: MoveUser
/// <summary>
/// 移动用户
/// </summary>
/// <param name="user_path">用户Path</param>
/// <param name="target_path">目标path</param>
/// <returns></returns>
public static string MoveUser(string user_path, string target_path)
{
DirectoryEntry u = new DirectoryEntry(user_path, ADUser, ADPassword);
DirectoryEntry t = new DirectoryEntry(target_path, ADUser, ADPassword);
u.MoveTo(t);
return u.Path;
}
示例14: MoveADObject
/// <summary>
/// Move an active directory organizational unit from one location to another.
/// </summary>
/// <param name="dnSource">The DN of the object that is going to be moved.</param>
/// <param name="dnDestination">The DN of the location where the OU will be placed.</param>
public static void MoveADObject(string dnSource, string dnDestination)
{
DirectoryEntry curOU = null;
DirectoryEntry destOU = null;
try
{
curOU = new DirectoryEntry($"LDAP://{dnSource}", Properties.Resources.DomainAccount, Properties.Resources.domain);
destOU = new DirectoryEntry($"LDAP://{dnDestination}", Properties.Resources.DomainAccount, Properties.Resources.domain);
curOU.MoveTo(destOU);
}
catch (Exception ex)
{
throw new Exception("An error occurred in MoveADOU. \n" + ex.Message);
}
finally
{
curOU?.Dispose();
destOU?.Dispose();
}
//curOU.Properties["description"].Add("Description");
}
示例15: MoveADObject
// possible not in use
public void MoveADObject(string objectLocation, string newLocation, LogFile log)
{
//For brevity, removed existence checks
// EXPECTS FULL Distinguished Name for both variables "LDAP://CN=xxx,DC=xxx,DC=xxx"
try
{
DirectoryEntry eLocation = new DirectoryEntry(objectLocation);
DirectoryEntry nLocation = new DirectoryEntry(newLocation);
string newName = eLocation.Name;
eLocation.MoveTo(nLocation, newName);
nLocation.Close();
eLocation.Close();
}
catch (Exception ex)
{
log.addTrn("failed to move ad object from " + objectLocation + " to " + newLocation + " exception " + ex.Message.ToString() + "\n" + ex.StackTrace.ToString(), "Error");
}
}