本文整理汇总了C#中System.DirectoryServices.DirectoryEntry.Rename方法的典型用法代码示例。如果您正苦于以下问题:C# DirectoryEntry.Rename方法的具体用法?C# DirectoryEntry.Rename怎么用?C# DirectoryEntry.Rename使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.DirectoryServices.DirectoryEntry
的用法示例。
在下文中一共展示了DirectoryEntry.Rename方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Rename
public static void Rename(string server,
string userName, string password, string objectDn, string newName)
{
DirectoryEntry child = new DirectoryEntry("LDAP://" + server + "/" +
objectDn, userName, password);
child.Rename("CN=" + newName);
}
示例2: SetPropertyValue
protected override void SetPropertyValue(IOguObject srcOguObject, string srcPropertyName, DirectoryEntry entry, string targetPropertyName, string context, SetterContext setterContext)
{
string srcPropertyValue = GetNormalizeddSourceValue(srcOguObject, srcPropertyName, context);
string targetPropertyValue = GetNormalizeddTargetValue(entry, targetPropertyName, context);
if (srcOguObject.FullPath == SynchronizeContext.Current.SourceRootPath)
{
srcPropertyValue = new PathPartEnumerator(SynchronizeContext.Current.TargetRootOU).Last(); //极其特别,不一定可靠,权限中心应限制更改这一组织的名称和位置。
}
else
{
string relativePath = SynchronizeHelper.GetRelativePath(srcOguObject);
if (relativePath.IndexOf('\\') < 0)
{
srcPropertyValue = SynchronizeContext.Current.GetMappedName(srcPropertyValue);
}
}
if (srcPropertyValue != targetPropertyValue && entry.IsBounded() == true)
{
TraceItHere(srcOguObject, srcPropertyName, entry, targetPropertyName, context, srcPropertyValue, targetPropertyValue);
entry.CommitChanges();
try
{
entry.Rename(srcOguObject.ObjectType.SchemaTypeToPrefix() + "=" + ADHelper.EscapeString(srcPropertyValue));
}
catch (DirectoryServicesCOMException ex)
{
if (ex.ErrorCode == -2147019886)
{
//对象已存在
entry.Rename(srcOguObject.ObjectType.SchemaTypeToPrefix() + "=TMP" + Environment.TickCount.ToString("X"));
SynchronizeContext.Current.DelayActions.Add(new DelayRenameAction(srcOguObject, entry.NativeGuid));
}
else
{
throw;
}
}
}
}
示例3: RenameMachineByDirectoryServices
public static int RenameMachineByDirectoryServices(String oldname, String newname, String administrator, String administratorPassword)
{
//PAS TERMINE
int res = 0;
try
{
DirectoryEntry machineNode = null;
machineNode = new DirectoryEntry("WinNT://" + oldname);
machineNode.Username = administrator;
machineNode.Password = administratorPassword;
machineNode.AuthenticationType = AuthenticationTypes.Secure;
machineNode.Rename("CN=" + newname);
machineNode.CommitChanges();
}
catch (Exception e)
{
String msg = e.Message;
String stacktrace = e.StackTrace;
}
return res;
}
示例4: DirectoryEntry_UsePropertyCache
//.........这里部分代码省略.........
using (DirectoryEntry barakTsabariDE = new DirectoryEntry(configuration.ServerRoot + "cn=Barak Tsabari,ou=R&D,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
configuration.Username,
configuration.Password,
configuration.AuthenticationType)){
Assert.AreEqual(barakTsabariDE.Properties["telephoneNumber"].Value,oldTelephoneNumber);
}
// restore object state
using (DirectoryEntry ouHumanResources = new DirectoryEntry( configuration.ServerRoot + "ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
configuration.Username,
configuration.Password,
configuration.AuthenticationType)){
using (DirectoryEntry barakTsabariDE = new DirectoryEntry(configuration.ServerRoot + "cn=Barak Tsabari,ou=R&D,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
configuration.Username,
configuration.Password,
configuration.AuthenticationType)){
barakTsabariDE.MoveTo(ouHumanResources);
barakTsabariDE.CommitChanges();
}
}
#endregion // Check MoveTo
#region Check Rename
// Rename not chached
de = new DirectoryEntry(barakTsabariDN,
configuration.Username,
configuration.Password,
configuration.AuthenticationType);
de.Rename("cn=MyUser");
try {
using (DirectoryEntry barakTsabariDE = new DirectoryEntry(barakTsabariDN,
configuration.Username,
configuration.Password,
configuration.AuthenticationType)){
barakTsabariDE.Properties["telephoneNumber"].Value = newTelephoneNumber;
barakTsabariDE.CommitChanges();
Assert.Fail("Object " + barakTsabariDN + " was not renamed on the server.");
}
}
catch(AssertionException ae) {
throw ae;
}
catch (Exception e) {
// do nothing
}
using (DirectoryEntry barakTsabariDE = new DirectoryEntry(configuration.ServerRoot + "cn=MyUser,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
configuration.Username,
configuration.Password,
configuration.AuthenticationType)){
Assert.AreEqual(barakTsabariDE.Properties["telephoneNumber"].Value,oldTelephoneNumber);
}
// restore object state
using (DirectoryEntry barakTsabariDE = new DirectoryEntry(configuration.ServerRoot + "cn=MyUser,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn)),
configuration.Username,
configuration.Password,
configuration.AuthenticationType)){
barakTsabariDE.Rename("cn=Barak Tsabari");
barakTsabariDE.CommitChanges();
示例5: DirectoryEntry_Rename
public void DirectoryEntry_Rename()
{
string barakTsabariOldDN = configuration.ServerRoot + "cn=Barak Tsabari,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
string barakTsabariNewDN = configuration.ServerRoot + "cn=My Name,ou=Human Resources,ou=people" + ((configuration.BaseDn.Length == 0) ? String.Empty : ("," + configuration.BaseDn));
using (DirectoryEntry barakTsabariDE = new DirectoryEntry( barakTsabariOldDN,
configuration.Username,
configuration.Password,
configuration.AuthenticationType)){
barakTsabariDE.Rename("cn=My Name");
barakTsabariDE.CommitChanges();
Assert.IsTrue(DirectoryEntry.Exists(barakTsabariNewDN));
barakTsabariDE.Rename("cn=Barak Tsabari");
barakTsabariDE.CommitChanges();
Assert.IsTrue(DirectoryEntry.Exists(barakTsabariOldDN));
}
}
示例6: ChangeOU
/// <summary>
/// 修改OU
/// </summary>
/// <param name="ouEntry"></param>
/// <param name="newOUName"></param>
/// <returns></returns>
public static bool ChangeOU(DirectoryEntry ouEntry, string newOUName)
{
//try
//{
ouEntry.Rename("OU=" + newOUName);
ouEntry.CommitChanges();
return true;
//}
//catch (Exception ex)
//{
// LogFactory.ExceptionLog.Error(ex);
// return false;
//}
}
示例7: HandleNameAndContainerChange
/// <summary>
/// </summary>
/// <param name="type"></param>
/// <param name="directoryEntry"></param>
/// <param name="attributes"></param>
/// <param name="config"></param>
private static void HandleNameAndContainerChange(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 oldName = directoryEntry.Name;
String newName = GetRelativeName(nameAttribute);
bool nameChanged = !NormalizeLdapString(oldName).Equals(NormalizeLdapString(newName), StringComparison.OrdinalIgnoreCase);
String oldContainer = GetParentDn(directoryEntry.Path);
String newContainer = GetParentDn(nameAttribute.GetNameValue());
bool containerChanged = !NormalizeLdapString(oldContainer).Equals(NormalizeLdapString(newContainer), StringComparison.OrdinalIgnoreCase);
if (!nameChanged && !containerChanged)
{
return;
}
if (nameChanged && !containerChanged) // rename without moving
{
LOGGER.TraceEvent(TraceEventType.Verbose, CAT_DEFAULT, "Renaming {0} to {1}", oldName, newName);
directoryEntry.Rename(newName);
LOGGER.TraceEvent(TraceEventType.Verbose, CAT_DEFAULT, "Rename OK");
return;
}
// so this is move with or without rename
// step 1: if WITH rename, we have to rename the entry to a temporary name first
String temporaryName = null;
if (nameChanged)
{
temporaryName = oldName + "-" + RandomStr();
LOGGER.TraceEvent(TraceEventType.Verbose, CAT_DEFAULT, "Renaming {0} to a temporary name of {1}", oldName, temporaryName);
directoryEntry.Rename(temporaryName);
LOGGER.TraceEvent(TraceEventType.Verbose, CAT_DEFAULT, "Rename OK");
}
// step 2: do the move
try
{
String newContainerLdapPath = ActiveDirectoryUtils.GetLDAPPath(config.LDAPHostName, newContainer);
DirectoryEntry newContainerDe = new DirectoryEntry(newContainerLdapPath, config.DirectoryAdminName, config.DirectoryAdminPassword);
LOGGER.TraceEvent(TraceEventType.Verbose, CAT_DEFAULT, "Moving from {0} to {1} ({2})", oldContainer, newContainer, newContainerLdapPath);
directoryEntry.MoveTo(newContainerDe);
LOGGER.TraceEvent(TraceEventType.Verbose, CAT_DEFAULT, "Move OK");
newContainerDe.Dispose();
}
catch (Exception e)
{
LOGGER.TraceEvent(TraceEventType.Information, CAT_DEFAULT, "Exception caught when moving: {0}", e);
if (nameChanged)
{
LOGGER.TraceEvent(TraceEventType.Information, CAT_DEFAULT, "Renaming back from temporary name of {0} to {1}", temporaryName, oldName);
directoryEntry.Rename(oldName);
LOGGER.TraceEvent(TraceEventType.Verbose, CAT_DEFAULT, "Rename OK");
}
throw e;
}
// step 3: if WITH rename, then rename from temporary name to the new name
if (nameChanged)
{
LOGGER.TraceEvent(TraceEventType.Verbose, CAT_DEFAULT, "Renaming from temporary name of {0} to a new name of {1}", temporaryName, newName);
directoryEntry.Rename(newName);
LOGGER.TraceEvent(TraceEventType.Verbose, CAT_DEFAULT, "Rename OK");
}
}
示例8: RenameObject
/// <summary>
/// rename an AD object
/// </summary>
/// <param name="objectDn"></param>
/// <param name="newName"></param>
/// <returns></returns>
public bool RenameObject(string objectDn, string newName)
{
try
{
DirectoryEntry child = new DirectoryEntry("LDAP://" + objectDn);
child.Rename("CN=" + newName);
return true;
}
catch (Exception ex)
{
Mailer.ErrNotify(ex, "ActiveDirectory RenameObject");
return false;
}
}
示例9: HandleNameChange
private static void HandleNameChange(UpdateType type,
DirectoryEntry directoryEntry,
ICollection<ConnectorAttribute> attributes)
{
Name nameAttribute = ConnectorAttributeUtil.GetNameFromAttributes(attributes);
if (nameAttribute != null)
{
// this only make sense for replace. you can't
// add a name or delete a name
if (type.Equals(UpdateType.REPLACE))
{
String oldName = directoryEntry.Name;
String newName = GetRelativeName(nameAttribute);
if (!NormalizeLdapString(oldName).Equals(NormalizeLdapString(newName), StringComparison.OrdinalIgnoreCase))
{
directoryEntry.Rename(newName);
}
}
}
}
示例10: Save
/// <summary>
/// Save the local ADUser data to the remote server
/// </summary>
public void Save()
{
if (isSaveable)
{
//Set password
if (passwordChanged)
{
_sourceUser.SetPassword(_password);
passwordChanged = false;
}
if (_sourceUser.Name == null || _sourceUser.DisplayName == null)
{
_sourceUser.Name = GivenName + " " + Surname;
_sourceUser.DisplayName = _sourceUser.Name;
}
Boolean wasNewUser = _isNewUser;
try
{
_sourceUser.Save();
//Save data to AD
if (_isNewUser)
{
directoryEntry = _sourceUser.GetUnderlyingObject() as DirectoryEntry;
}
_isNewUser = false;
}
catch (PrincipalExistsException ex)
{
throw new DuplicateUser(ex.Message);
}
Sync_Groups();
//Have to reload the UserPrincipal to get the DirectoryEntry
UserPrincipal newSourceUser = _connection.GetUserBySid(_sourceUser.Sid);
//if (wasNewUser)
//{
if (newSourceUser == null)
{
string msg = "Couldn't reload user: " + _sourceUser.DisplayName;
throw new InvalidOperationException(msg);
}
_sourceUser = newSourceUser;
//}
if (directoryEntry != null)
{
directoryEntry.Close();
}
directoryEntry = _sourceUser.GetUnderlyingObject() as DirectoryEntry;
directoryEntry.Options.SecurityMasks = SecurityMasks.Dacl;
//Copy local attributes to directory entry
//directoryEntry.RefreshCache(DEFields.ToArray<string>());
mapper.Copy(this, directoryEntry);
if ((this.Manager != null) && (!this.Manager.DistinguishedName.Equals(directoryEntry.Properties["manager"].Value)))
{
if (directoryEntry.Properties.Contains("manager"))
{
directoryEntry.Properties["manager"].Value = this.Manager.DistinguishedName;
}
else
{
directoryEntry.Properties["manager"].Add(this.Manager.DistinguishedName);
}
}
if (DisplayName != directoryEntry.Properties["cn"].Value as string)
{
directoryEntry.Rename("CN=" + DisplayName);
}
try
{
directoryEntry.CommitChanges();
}
catch (DirectoryServicesCOMException ex)
{
System.Diagnostics.Debug.Write(ex.Message + " - " + ex.StackTrace);
throw new ADException(String.Format("DirectoryServicesCOMException {0}", ex.Message), ex);
}
if (this.HideFromAddressLists)
{
try
{
if (!directoryEntry.Properties.Contains("msExchHideFromAddressLists"))
//.........这里部分代码省略.........
示例11: RenameOU
/// <summary>
/// 重命名OU
/// </summary>>
/// <param name="oldOUName">原名</param>
/// <param name="newOUName">新名</param>
/// <returns>是否成功</returns>
public static bool RenameOU(string oldOUName, string newOUName)
{
try
{
if (CheckOU(oldOUName))
{
DirectoryEntry userEntry = new DirectoryEntry(GetOrganizeNamePath(oldOUName), ADUser, ADPassword);
userEntry.Rename("OU=" + newOUName);
userEntry.CommitChanges();
userEntry.Dispose();
if (CheckOU(newOUName))
return true;
else
return false;
}
else
return false;
}
catch (Exception)
{
return false;
//throw;
}
}
示例12: DisableADObjectCustomProperties
public static void DisableADObjectCustomProperties(DirectoryEntry entry, List<PropertyMapping> propertyMappings, int ADNameMaxLength, int ADsAMAccountNameMaxLength)
{
// entry.name
var deletedEntryName = GetADObjectName(entry.Name).PrefixDeleted();
var newName = string.Concat(GetADObjectPrefix(ADObjectType.User), deletedEntryName.MaximizeLength(ADNameMaxLength));
entry.Rename(newName);
// sAMAccountName
entry.Properties[SyncConfiguration.GetUserNameProp(propertyMappings)].Value = deletedEntryName.MaximizeLength(ADsAMAccountNameMaxLength);
foreach (PropertyMapping propMapping in propertyMappings)
{
foreach (SyncProperty adProp in propMapping.ADProperties)
{
if (adProp.Unique)
{
var propValue = GetEntryValue(entry, adProp).PrefixDeleted();
SetEntryValue(entry, adProp, propValue);
}
}
}
}
示例13: RenameADObjectIfNecessary
public static void RenameADObjectIfNecessary(DirectoryEntry entry, Node node, int ADNameMaxLength, bool allowRename)
{
if (!allowRename)
return;
if (GetADObjectName(entry.Name) != node.Name)
{
var nodeNameMax = node.Name.MaximizeLength(ADNameMaxLength);
var name = string.Concat(GetADObjectPrefix(GetADObjectType(node.NodeType)), nodeNameMax);
entry.Rename(name);
}
}
示例14: RenameObject
/// <summary>
/// Renames an object.
/// </summary>
/// <param name="objDN">The distinguished name of the object to rename.</param>
/// <param name="newName">The new name for the object.</param>
public static void RenameObject(string objDN, string newName)
{
try
{
var curObj = new DirectoryEntry($"LDAP://{objDN}", Properties.Resources.DomainAccount, Properties.Resources.domain);
newName = newName.Replace(",", "\\,");
curObj.Rename($"CN={newName}");
curObj.CommitChanges();
}
catch (Exception ex)
{
throw new Exception($"Could not rename \"{objDN}\" to \"{newName}\". The following error occurred: {ex.Message}");
}
}