PDA

View Full Version : وصل شده به اکتیو دایرکتوری با c# (سوال)



armin.m
یک شنبه 01 اردیبهشت 1392, 16:51 عصر
سلام
من میخوام به اکتیو دایرکتوری ویندوز متصل شم و براش User & pass بفرستم و در برابر اکتیو به من درست یا غلط بودن یوزر نیم پسورد رو برگردونه. یعنی فرض کنید تو بخشی از برنامه ی من کاربر یوزر نیم پسورد خودش تو اکتیو رو میزنه و بعد برنامه ی من یوزر و پس رو میفرسته برای اکتیو ، و بعد اکتیو جوابش رو میده که یوزر و پس درسته یا نه. اگر یوزر و پس درست بود برنامه اجازه ی ادامه ی کار به کاربر رو بده. حالا سوال اینجاس که من چه جوری باید به اکتیو وصل شم ، چه جوری براش اطلاعات بفرستم و چه چجوری دریافت کنم!
از یکی در این مورد پرسیدم گفت باید از API اکتیو استفاده کنی. البته من چیز زیادی نفهمیدم!
پیشاپیش ممنون! :)

linux
سه شنبه 03 اردیبهشت 1392, 00:17 صبح
using System;
using System.Text;
using System.Collections;
using System.DirectoryServices;

namespace FormsAuth
{
public class LdapAuthentication
{
private String _path;
private String _filterAttribute;

public LdapAuthentication(String path)
{
_path = path;
}

public bool IsAuthenticated(String domain, String username, String pwd)
{
String domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);

try
{ //Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;

DirectorySearcher search = new DirectorySearcher(entry);

search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();

if(null == result)
{
return false;
}

//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}

return true;
}

public String GetGroups()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn=" + _filterAttribute + ")";
search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();

try
{
SearchResult result = search.FindOne();

int propertyCount = result.Properties["memberOf"].Count;

String dn;
int equalsIndex, commaIndex;

for(int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
{
dn = (String)result.Properties["memberOf"][propertyCounter];

equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if(-1 == equalsIndex)
{
return null;
}

groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
groupNames.Append("|");

}
}
catch(Exception ex)
{
throw new Exception("Error obtaining group names. " + ex.Message);
}
return groupNames.ToString();
}
}
}

armin.m
چهارشنبه 04 اردیبهشت 1392, 17:47 عصر
سلام
متشکرم! هنوز وقت نکردم تستش کنم ولی اگه به مشکل برخوردم بازم ازتون کمک می گیریم ! ممنون :)
فقط یه مسئله ای. این کد الان وصل میشه به API اکتیو؟! API اکتیو رو باید دانلود کرد و نصبش کرد یا خودش داره؟!
مرسی