update 20241211
This commit is contained in:
148
ADDemo/Form1.cs
Normal file
148
ADDemo/Form1.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.DirectoryServices;
|
||||
using System.DirectoryServices.Protocols;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
|
||||
namespace ADDemo
|
||||
{
|
||||
public partial class Form1 : Form
|
||||
{
|
||||
string Username = "";
|
||||
string ADError = "";
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
{
|
||||
DirectoryEntry de = new DirectoryEntry();
|
||||
Get_User("meyering", ref de);
|
||||
}
|
||||
|
||||
public class UserData
|
||||
{
|
||||
public string Name { get; set; } = "";
|
||||
public string Department { get; set; } = "";
|
||||
public string Office { get; set; }
|
||||
|
||||
}
|
||||
|
||||
private void trylogin()
|
||||
{
|
||||
try
|
||||
{
|
||||
LdapConnection connection = new LdapConnection("ldap.fabrikam.com");
|
||||
NetworkCredential credential = new NetworkCredential("user", "password");
|
||||
connection.Credential = credential;
|
||||
connection.Bind();
|
||||
Console.WriteLine("logged in");
|
||||
}
|
||||
catch (LdapException lexc)
|
||||
{
|
||||
String error = lexc.ServerErrorMessage;
|
||||
Console.WriteLine(lexc);
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Console.WriteLine(exc);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------
|
||||
using (DirectoryEntry adsEntry = new DirectoryEntry(path, strAccountId, strPassword))
|
||||
{
|
||||
using (DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry))
|
||||
{
|
||||
//adsSearcher.Filter = "(&(objectClass=user)(objectCategory=person))";
|
||||
adsSearcher.Filter = "(sAMAccountName=" + strAccountId + ")";
|
||||
|
||||
try
|
||||
{
|
||||
SearchResult adsSearchResult = adsSearcher.FindOne();
|
||||
bSucceeded = true;
|
||||
|
||||
strAuthenticatedBy = "Active Directory";
|
||||
strError = "User has been authenticated by Active Directory.";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Failed to authenticate. Most likely it is caused by unknown user
|
||||
// id or bad strPassword.
|
||||
strError = ex.Message;
|
||||
}
|
||||
finally
|
||||
{
|
||||
adsEntry.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private void getusera()
|
||||
{
|
||||
UserData user = new UserData();
|
||||
DirectoryEntry entry = new DirectoryEntry("LDAP://DC=mydomain,DC=local");
|
||||
using (DirectorySearcher ds = new DirectorySearcher(entry))
|
||||
{
|
||||
ds.Filter = "(SAMAccountName=" + "myusername" + ")";
|
||||
ds.PropertiesToLoad.Add("displayName");
|
||||
ds.PropertiesToLoad.Add("name");
|
||||
ds.PropertiesToLoad.Add("department");
|
||||
ds.PropertiesToLoad.Add("physicalDeliveryOfficeName");
|
||||
var result = ds.FindOne();
|
||||
if (result != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
user.Name = result.Properties["name"][0].ToString();
|
||||
user.Department = result.Properties["department"][0].ToString();
|
||||
user.Office = result.Properties["physicalDeliveryOfficeName"][0].ToString();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
public bool Get_User(string Userid, ref DirectoryEntry de)
|
||||
{
|
||||
var enTry = new DirectoryEntry("LDAP://db.debian.org/uid=meyering,ou=users,dc=debian,dc=org");
|
||||
// Dim enTry As DirectoryEntry = New DirectoryEntry("LDAP://db.debian.org/uid=abi,ou=users,dc=example,dc=com")
|
||||
|
||||
var mySearcher = new DirectorySearcher(enTry);
|
||||
mySearcher.Filter = "(SAMAccountName=" + Userid + ")";
|
||||
foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
|
||||
{
|
||||
try
|
||||
{
|
||||
de = resEnt.GetDirectoryEntry();
|
||||
Username = de.Properties["givenname"].ToString() + de.Properties["sn"].ToString();
|
||||
ADError = "";
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.Username = "";
|
||||
|
||||
this.ADError = ex.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user