Click here to Skip to main content
15,886,565 members
Home / Discussions / C#
   

C#

 
AnswerRe: writing a development environment Pin
Pete O'Hanlon10-Jun-09 23:25
mvePete O'Hanlon10-Jun-09 23:25 
Questionapp config Pin
Cyrus-IRA10-Jun-09 13:43
Cyrus-IRA10-Jun-09 13:43 
AnswerRe: app config Pin
Leonscape10-Jun-09 14:50
Leonscape10-Jun-09 14:50 
GeneralRe: app config Pin
Cyrus-IRA10-Jun-09 15:56
Cyrus-IRA10-Jun-09 15:56 
QuestionFlickering. Pin
Baeltazor10-Jun-09 12:57
Baeltazor10-Jun-09 12:57 
QuestionActive directory Question Pin
caiena10-Jun-09 11:12
caiena10-Jun-09 11:12 
AnswerRe: Active directory Question Pin
Leonscape10-Jun-09 11:29
Leonscape10-Jun-09 11:29 
AnswerRe: Active directory Question Pin
saurabh sahay10-Jun-09 12:12
saurabh sahay10-Jun-09 12:12 
Login Using Active Directory Services (ADS)

In order to use SDS, first we have to set properties of LDAP server. Here database ADSI_PARAMETER table is used to set the LDAP properties.

CREATE TABLE [dbo].[ADSI_PARAMETER] (
[ParameterName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[ParameterValue] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO

Enter following data to table in ParameterName and ParameterValue fields

Parameter Name ParameterValue
ServerName WindowsDomainServer
BaseDN DC=DomainName,DC=COM
UserDN OU=Users
GroupName CN=Operater, OU=Groups
AccountFilter sAMAccountName


Where ServerName is your domain server machine name. BaseDN is your domain name, most of the time it is company name. UserDN is organizational unit where user should exist. GroupName is organizational unit, to which user should belong in order to access your software. AccountFilter is filter for account name; mostly it is sAMAccountName in windows.



Code for GetADSILogin function.

public void GetADSILogin()
{
try
{
string strServerName = "";
string strBaseDN = "" ;
string strUserDN = "";
string strGroupName = "";
string strAccountFilter = "";
//Port no for LDAP Default is 389
string strPortNo = "389";
Boolean blnGroupUser=false;
//Data source string
string source = "Data Source=ATHAKUR;Initial Catalog=Times;user=sa;password=sa" ;
//SQL statement that will be issued
string select = "SELECT * from ADSI_PARAMETER";
//SQL Connection
SqlConnection conn=new SqlConnection(source);
// Open the database connection
conn.Open () ;
// Create the SQL command...
SqlCommand cmd = new SqlCommand ( select , conn ) ;
//Execute Data reader
SqlDataReader myReader = cmd.ExecuteReader();
//Check if any rows return against user/pass
if(myReader.HasRows)
{
while(myReader.Read())
{
//Store the parameter's data in variables
string strParameterName = myReader.GetString(0).Trim();
string strParameterValue = myReader.GetString(1).Trim();

if(strParameterName.ToUpper().Equals("SERVERNAME"))
strServerName=strParameterValue;
if(strParameterName.ToUpper().Equals("BASEDN"))
strBaseDN=strParameterValue;
if(strParameterName.ToUpper().Equals("USERDN"))
strUserDN=strParameterValue;
if(strParameterName.ToUpper().Equals("GROUPNAME"))
strGroupName=strParameterValue;
if(strParameterName.ToUpper().Equals("ACCOUNTFILTER"))
strAccountFilter=strParameterValue;
}
}
//Search for user
DirectoryEntry deSystem = new DirectoryEntry("LDAP://" + strServerName + "/" + strUserDN + ","
+ strBaseDN);
deSystem.AuthenticationType=AuthenticationTypes.Secure;
deSystem.Username=txtUserName.Text;
deSystem.Password =txtPassword.Text;
//Search for account name
string strSearch=strAccountFilter + "=" + txtUserName.Text;
DirectorySearcher dsSystem = new DirectorySearcher(deSystem,strSearch);
//Search subtree of UserDN
dsSystem.SearchScope= SearchScope.Subtree;
//Find the user data
SearchResult srSystem = dsSystem.FindOne();
//Pick up the user group belong to
ResultPropertyValueCollection valcol = srSystem.Properties["memberOf"];
if(valcol.Count>0)
{
foreach( object o in valcol )
{
//check user exist in Group we are searching for
if(o.ToString().Equals(strGroupName+","+strBaseDN))
{
blnGroupUser=true;
break;
}
}
}
if(blnGroupUser==true)
MessageBox.Show("Login Sucessfull...");
else
MessageBox.Show("User Does Not Belong to Specified ADSI Group");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
i=i+1;
if(i==5)
{
MessageBox.Show("Login failed for 5 times. Quiting...");
this.Close();
}
}
If everything works fine then you will get the message "Login Successful". If user does not belong to group specified in LDAP properties then will the message "User Does Not Belong to Specified ADSI Group". If you enter wrong user/pass, you will get Logon failure message.
GeneralRe: Active directory Question Pin
Dave Kreskowiak10-Jun-09 17:14
mveDave Kreskowiak10-Jun-09 17:14 
QuestionType declaration ? Pin
daveyerwin10-Jun-09 9:55
daveyerwin10-Jun-09 9:55 
AnswerRe: Type declaration ? Pin
0x3c010-Jun-09 10:12
0x3c010-Jun-09 10:12 
AnswerRe: Type declaration ? Pin
Leonscape10-Jun-09 11:20
Leonscape10-Jun-09 11:20 
AnswerRe: Type declaration ? Pin
Luc Pattyn10-Jun-09 11:20
sitebuilderLuc Pattyn10-Jun-09 11:20 
QuestionC# need some direction Pin
marksmit10-Jun-09 8:48
marksmit10-Jun-09 8:48 
AnswerRe: C# need some direction Pin
Henry Minute10-Jun-09 8:58
Henry Minute10-Jun-09 8:58 
AnswerRe: C# need some direction Pin
Christian Graus10-Jun-09 9:04
protectorChristian Graus10-Jun-09 9:04 
AnswerRe: C# need some direction Pin
sgenie6810-Jun-09 17:16
sgenie6810-Jun-09 17:16 
GeneralThanks guys! Pin
marksmit11-Jun-09 4:53
marksmit11-Jun-09 4:53 
QuestionThoroughly confused now :) Pin
marksmit11-Jun-09 5:49
marksmit11-Jun-09 5:49 
Question[Message Deleted] Pin
hkjghkj110-Jun-09 8:28
hkjghkj110-Jun-09 8:28 
AnswerRe: parallel sounds Pin
Christian Graus10-Jun-09 8:44
protectorChristian Graus10-Jun-09 8:44 
Questionif statement troubles Pin
partialdata10-Jun-09 6:54
partialdata10-Jun-09 6:54 
GeneralRe: if statement troubles Pin
harold aptroot10-Jun-09 7:14
harold aptroot10-Jun-09 7:14 
GeneralRe: if statement troubles Pin
partialdata10-Jun-09 8:04
partialdata10-Jun-09 8:04 
AnswerRe: if statement troubles Pin
Luc Pattyn10-Jun-09 7:25
sitebuilderLuc Pattyn10-Jun-09 7:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.