Laden...

ASP.NET (C#) & Active Directory - User Properties

Erstellt von sspaeti vor 17 Jahren Letzter Beitrag vor 17 Jahren 7.729 Views
S
sspaeti Themenstarter:in
1 Beiträge seit 2006
vor 17 Jahren
ASP.NET (C#) & Active Directory - User Properties

Ich möchte gerne die Properties eines Users in der OU "WebIDA" ändern. Dieses mache ich mit der Methode "SetProperty". Bei dieser Methode wird DirectoryEntry, das Propertie und der neue Value angegeben. Mein Prolem ist jetzt aber, wie erhalte ich den User als DirectoryEntry Objekt?

Wenn ich SetProperty(de, "givenName", "sepp") mache, ändere ich ja das Objekt de welche die OU struktur enthaltet. Aber ich weiss nicht welches Objekt ich angeben muss, um den User zu ändern und nicht das OU objekt.

Kann mir bitte jemand helfen? Es ist bestimmt eine einfache Frage... Ich habe schon das ganze Internet durchforscht und versuche seit 2 Wochen dieses zu schaffen, aber ich bin einfach zu blöd...

Vielen Dank schon im voraus!

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;
using System.Globalization;
using System.Security.Principal;
using System.Web.Services.Description;
//using DirectoryUtility.DirectoryServices;


//namespace DirectoryUtility


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
GetDirectoryEntry();
UserExists(textbox_user.Text);

}


//1. Create a connection to Active Directory
/// 
/// Method used to create an entry to the AD.
/// Replace the path, username, and password.
/// 
/// DirectoryEntry
public static DirectoryEntry GetDirectoryEntry()
{
DirectoryEntry de = new DirectoryEntry();
//de.Path = "LDAP://192.168.1.1/CN=WebIDA;DC=Main";
de.Path = "LDAP://sv01/OU=WebIDA,DC=Main,DC=local";
de.Username = @"Main\programmer";
de.Password = "123asd$";
de.AuthenticationType = AuthenticationTypes.Secure;
return de;
}

//2. Create a secure connection to Active Directory

/// 
/// Establish identity (principal) and culture for a thread.
/// 
/*public static void SetCultureAndIdentity()
{
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
WindowsIdentity identity = (WindowsIdentity)principal.Identity;
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
}*/

//3. Validate if a user exists

/// 
/// Method to validate if a user exists in the AD.
/// 
/// 
/// 
public bool UserExists(string UserName)
{
DirectoryEntry de = GetDirectoryEntry();
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = "(&(objectClass=user) (cn=" + UserName + "))";
SearchResultCollection results = deSearch.FindAll();

if (results.Count == 0)
{
//test_button.Visible = false;
Label1.Text = "User besteht nicht!";
return false;
}
else
{

//List all AD-account Properties
PopulateProperties(de);

//test_button.Visible = true;
Label1.Text = "User besteht";
return true;


}
}
//4. Set user's properties

/// 
/// Helper method that sets properties for AD users.
/// 
/// 
/// 
/// 
public static void SetProperty(DirectoryEntry de, string PropertyName, string PropertyValue)
{
if (PropertyValue != null)
{
if (de.Properties.Contains(PropertyName))
{
de.Properties[PropertyName][0] = PropertyValue;
}
else
{
de.Properties[PropertyName].Add(PropertyValue);
}
}
}

//gibt die properties des accounts userenty aus
public void PopulateProperties(DirectoryEntry userEntry)
{
if (userEntry != null)
{
//propertiesListBox.clear();
foreach (string propertyName in userEntry.Properties.PropertyNames)
{
//string propertyName = userEntry.Password.ToString;
propertiesListBox.Items.Add(propertyName);

//Property Ausgabe (Listbox)
PropertyOutput(userEntry, propertyName);
}
}
}

//gibt den Wert der Properties ind PopulateProperties aus
public void PropertyOutput(DirectoryEntry de, string propertyName)
{
//string loginID = de.Properties["sAMAccountName"].Value.ToString();
string strPropertie = de.Properties[propertyName].Value.ToString();
//MessageBox.Show("Login ID: " + loginID.ToString() + "\r\n Full Name " + firstName.ToString(), "Results");

ListBox1.Items.Add(strPropertie);
}

}