PDA

View Full Version : سوال: ورود به برنامه با استفاده از کاربران در Active Directory



MasoudAdmin
سه شنبه 23 فروردین 1401, 20:13 عصر
با سلام خدمت همه
من یک برنامه دارم که وصل Active Directory میشه و نام کاربری رو میخونه فقط واسه رمز عبورش که کاربر وارد میکنه چجوری باید پسورد کاربر رو از Active Directory بخونم و وقتی کاربر رمزش رو زد بره سمت Active Directory و اگه نام کاربری و رمزش صحیح بود اجازه ورود به برنامه رو بده.:متفکر:
ممنون میشم راهنمایی کنید.

MasoudAdmin
جمعه 26 فروردین 1401, 18:54 عصر
با سلام.
دوستان خودم یافتم:
این کد لیست کاربران در Active Directory

this.radGridViewListUsersLDAP.Rows.Clear();
using (var context = new PrincipalContext(ContextType.Domain, "DomainName"))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context) { Enabled = true }))
{
int i = 1;
foreach (var result in searcher.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
this.radGridViewListUsersLDAP.Rows.Add(i.ToString( ), de.Properties["displayName"].Value, de.Properties["givenName"].Value,
de.Properties["sn"].Value, de.Properties["samAccountName"].Value, de.Properties["distinguishedName"].Value);
i++;
}
}
}


اینم لیست چک رمز:


public bool ResultLDAPUser
{
get
{
bool Result = true;
try
{
LdapConnection connection = new LdapConnection("ِDomainName");
NetworkCredential credential = new NetworkCredential(this.cmbUserName.Text.Trim(), this.txtPassword.Text.ToString());
connection.Credential = credential;
connection.Bind();
Result = true;
}
catch (LdapException lexc)
{
Result = false;
String error = lexc.ServerErrorMessage;
FarsiMessegeBox.Show("نام كاربري و يا رمز عبور اشتباه مي باشد", "توجّه", FMessegeBoxButtons.Ok, FMessegeBoxIcons.Error, FMessegeBoxDefaultButton.button2);
}

return Result;
}
}


البته باید از این کتابخانه ها هم استفاده کنید و DLL رو تو پروژه اضافه کنید:
using System.Net;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
using System.DirectoryServices.AccountManagement;