473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C# Form Authentication with LDAP AD

Hi All,

I planned the Form authenticaon with ldap i assigned to code and works fine,

but i want to Filter security group member, only sign in "Messenger" group
in my web site, i used msdn library "GetGroups" otpion for "memberOf" object.
but it's not security group filter. all users login my web site this my code
below;

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()
{
DirectoryEntry entr = new
DirectoryEntry("GC://gmdsidc1.msft.local/dc=msft,dc=Local", "msft.local" +
@"\" + "mutluozel", "1q2w3e4r");

DirectorySearcher search = new DirectorySearcher(entr);
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("Messenger");
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining group names. " +
ex.Message);
}
return groupNames.ToString();
}

}
}

Nov 11 '07 #1
0 2447

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: dmcconkey | last post by:
Hi folks, I've been searching for a while and haven't found my specific question anywhere else. If this has already been asked, please accept my appologies and point me to the appropriate...
0
by: Srinivas | last post by:
Hi, I've one system in which Active directory is installed. That system is the domain controller as well as web server - A test machine. Trying all following to Authenticate Users using VB as...
4
by: David Moore | last post by:
Hello I am using the System.DirectoryServices namespace classes to access Active Directory. We connect using the LDAP://DOMAIN method. The code works on local dev boxes, and in staging, but...
5
by: DSISupport | last post by:
Hi, I'm planing to use the enterprise library in a new web project, and I was looking at the Security application block which came by defaul with one provider called database authentication...
0
by: Grey | last post by:
I have set up domain controller with Windows 2K Server. I want to design a form authentication with this server. I read from some reference code that it is necessary to spcificed the LDAP path in...
2
by: duncan beaumont | last post by:
Hi, - SQLserver 2000 - Yellowfin 2.4 - Windows 2003 server I have been asked to investigate seting up LDAP authentication to access our Yellowfin reporting.
0
by: jayadevi | last post by:
Is it possible to configure an IIS to do both LDAP authentication and Windows authentication? For ex: Suppose one application of server A is hosted in IIS. The requests come from two different...
18
by: troywalker | last post by:
I am new to LDAP and Directory Services, and I have a project that requires me to authenticate users against a Sun Java System Directory Server in order to access the application. I have found...
40
by: webrod | last post by:
Dear All, let's say I have a web service. I would like to authenticate users who try to access it. I am on a winnt server so I will have to use NTLM but I don't want to use IIS settings. Is...
0
by: Kashgarinn | last post by:
Greetings. I'm in a bit of a pickle.. at the company where I recently started work, I discovered that the knowledge base we use sucks, and sucks bad (oh Lotus Notes, how I loathe thee, let me...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.