Archive

Posts Tagged ‘c#’

User validation with LDAP in C# (2005)

April 11, 2012 Leave a comment

Once in my project I came to a problem where I need to validate a user with LDAP server. My project was in C# (2005). I have searched a lot in Google and at the end I got a working solution. There is another solution as well which does not worked for me but I guess for somebody else it might. So, I’ll present both of them here:

First the one which is widely used (but didn’t worked for me :( )

For this one you need to add a reference to System.DirectoryServices in your code.


public static bool ValidateUserCredentials(string _domain, string _userName, string _password)
{
DirectoryEntry nRoot = null;

try
{
nRoot = new DirectoryEntry("YOUR_LDAP_SERVER_PATH", _domain + "//" + _userName, _password);
//You can omit the domain if you want
nRoot.AuthenticationType = AuthenticationTypes.None;

Object obj = nRoot.NativeObject;

DirectorySearcher search = new DirectorySearcher(nRoot);

search.SearchScope = SearchScope.Subtree;
search.Filter = "(sAMAccountName=" + _userName + ")";
//You can specify the properties to Load
//search.PropertiesToLoad.Add("uid");

SearchResult _sr = search.FindOne();

if (null != _sr)
{
ResultPropertyCollection myResultPropColl = _sr.Properties;

foreach (string myKey in myResultPropColl.PropertyNames)
{
string tab = "    ";
Console.WriteLine(myKey + " = ");

foreach (Object myCollection in myResultPropColl[myKey])
{
string _val = tab + myCollection;
}
}
return true;
}
else
{
throw new Exception("User Not found");
}

}
catch (Exception _exp)
{
throw _exp;
}
finally
{
if (null != nRoot)
nRoot.Close();
}
}

And, following is the one worked for me.

For this one you need to add System.DirectoryServices.AccountManagement namespace in your reference.

 

public static bool ValidateUserCredentials(string _domain, string _userName, string _pwd)
{
try
{
PrincipalContext pc = new PrincipalContext(ContextType.Domain, _domain);

bool isValid = pc.ValidateCredentials(_userName, _pwd);
return isValid;

}
catch (Exception _Exp)
{
throw _Exp;
}
}

Don’t forget to tell me which one works for you :)

How to get connection strings to various databases in ASP.NET 2.0

September 9, 2008 Leave a comment

How to use connection strings to various Databases in ASP.NET 2.0

Some of the values and strings are used across all the pages of a website. It is a tedious process to code those values and strings in each page of the website.

The connection string will be used in many pages of the web site if you are developing a database driven website. Let us say that you have hard coded the connection string in all the pages, around 30 pages. Now if the value of the connection string changes, you have a headache of changing all the values of the connection string in all the 30 pages. This can’t be done easily and if you are to change the string again after about one month, it is going to be a tedious task.

Hence in such scenarios it is a practice to store those values in a central repository from where you can retrieve it and use it in all the pages. For example if you store the connection string in a central place like a web.config file you can retrieve those connection strings from the web.config file and use it. Previous to ASP.Net 2.0 the connection strings were stored in web.config file as given below:

<configuration>
<appSettings>
<add key=”connectionString” value=”connection_string_value_here” />
</appSettings>

<system.web>

</system.web>
</configuration>

The connection string is stored as key and value pair in the <appSettings> element of the web.config file. This value can be retrieved by writing code like:

ConfigurationSettings.AppSettings(“connectionString”)

Instead of using the <appSettings> section you can also add your own sections in the web.config file to have all your string value that you will be using in your application.

ASP.Net 2.0 provides with a special class for retrieving those customized string values.

This class is called the ConfigurationManager class.

Using this class you will be retrieving values by writing the following code for the above config file.

ConfigurationManager.AppSettings["connectionString"]

To open the ConnectionStrings section of the web.config file, you can use code like

ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString

The above code is used to retrieve ConnectionStrings from the web.config file as given below:

<connectionStrings>
<add name=”connectionString” connectionString=”Data Source=.;Initial Catalog=db_products;Integrated Security=True” providerName=”System.Data.SqlClient” />
</connectionStrings>

This code comes under the <configuration> section of the web.config file.

Here you can use the connectionStrings tag to store multiple Connection string to multiple databases and use it at your might.

Like:

<connectionStrings>
<add name=”connectionString” connectionString=”Connect_String_Value” providerName=”Connection_Provider” />

<add name=”myCString” connectionString=”Connect_String_Value” providerName=”Connection_Provider” />

<add name=”newCString” connectionString=”Connect_String_Value” providerName=”Connection_Provider” />
</connectionStrings>

You can use it in you code like :

1. ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString

2. ConfigurationManager.ConnectionStrings["myCString"].ConnectionString

3. ConfigurationManager.ConnectionStrings["newCString"].ConnectionString


Happy Connecting to database :)

Follow

Get every new post delivered to your Inbox.

Join 29 other followers