Friday, April 4, 2008

interacting with Active Directory


Recently I had to work on a web application that authenticated via Active Directory/IIS. In other words in IIS, anonymous access was turned off for my app, and Windows Authentication was turned on (image shows default setting for a web app; for this situation you need to uncheck anonymous access and check windows integration). When you do this, you then set at the folder level in Windows explorer who has access. It is at the folder level that it was indicated, in my case, to use Active Directory. For the current ePortfolio, on the other hand, users were added, account by account, to the folder via the Security tab.


Here are some articles on the topic:

Integrated Security with Web Application

Accessing the Active Directory from Microsoft .NET

Howto: (Almost) Everything In Active Directory via C#

Now I didn't really use the methods describes in these articles because they all demonstrate a way to interact with Active Directory that involves connecting directly to the Active Directory server. In my situation, I was not able to obtain the information needed to create the connection (LDAP server info). So below I will show you the code I did use.


string strADUser = Request.ServerVariables["LOGON_USER"].ToString().Substring(Request.ServerVariables["LOGON_USER"].ToString().LastIndexOf("\\") + 1);


Note that for ePortfolio Dave accessed the "HttpContext.Current.User.Identity.Name" property. I tried this first but it appears AD does not populate this property.

The last of my code:

Here I compare the SAMAccountName of the logged in user, to see if that person is a member of a specific AD group:


DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(SAMAccountName={0})", strADUser);
search.PropertiesToLoad.Add("memberOf");

SearchResult result = search.FindOne();
if (result != null)
{
int groupCount = result.Properties["memberOf"].Count;

for (int counter = 0; counter < groupCount; counter++)
{
string str = (string)result.Properties["memberOf"][counter];
if (str.Contains(editGroupName))
{
return true;
}
}
}


Any questions, suggestions for improvement? Post to the comments!

Tuesday, April 1, 2008

transaction

I am always looking through our code for a previous example of using transactions in .NET, so I am posting the code here. That's what here is for!



private void TRANSACTION EXAMPLE(int example)
{
using (SqlConnection conn = new SqlConnection(dsn))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
SqlTransaction transaction;

transaction = conn.BeginTransaction("GiveItAName");
cmd.Connection = conn;
cmd.Transaction = transaction;
try
{
//1st query, initializes sql variable
string sql = "INSERT INTO someTable etcetc";
cmd.CommandText = sql;
//define needed parameters is any
cmd.ExecuteNonQuery();

//next query
sql = "UPDATE or WHATEVER QUERY etc etc";
cmd.CommandText = sql;

// define needed parameters
// NOTE you don't have to define any param if it was already defines and used for a previous query
cmd.Parameters.Add("@something", SqlDbType.VarChar, 50).Value = exaple;
cmd.ExecuteNonQuery();

//next query
sql = "UPDATE etc etc";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

// Attempt to commit the transaction.
transaction.Commit();
}
catch (Exception)
{
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
throw;
}
throw;
}
}
}

Thursday, March 13, 2008

adding using statements

anyone know a good shortcut for adding it to all cs files in a project in Visual Studio?

Wednesday, March 12, 2008

Calculation in an UPDATE statement

This is how you do it:
UPDATE myTable SET myField = myField + 1 WHERE myId = XXX

Tuesday, March 11, 2008

Store application information in web.config

To store application information in web.config is easy:

In Web.config

<appsettings>
<add value="xxx@xxx.com" key="adminEmail">
</appsettings>

In the program, this is how you retrieve the information:

string sEmail = System.Web.Configuration.WebConfigurationManager.AppSettings["adminEmail"].ToString();

Storing this type of info in the web.config allow you to set it at anytime without recompiling your program. So when you are testing, set it to your own email address so you will set the email. I am tired of getting and deleting the testing emails :)

Friday, March 7, 2008

Images and Clipart

Good morning every,
I have been doing some research on images and vector images (clip art) for us to use for stock photography for our web site development. I found this site from microsoft.

http://office.microsoft.com/en-us/clipart/FX101321031033.aspx

This link allows you to download photographs, vector images, and some other stuff. I downloaded the organizer and it worked very well for me. It is also free, or at least I didn't get charged.

Might be good for us when we need pictures. Let me know if you have any questions...

Wednesday, March 5, 2008

CSS Shorthand Guide

I was looking up some CSS help for displaying background images correctly in IE and Firefox and I came across this:

http://www.dustindiaz.com/css-shorthand/

Its a CSS Shorthand guide that some of you may want to look at. I havent had a chance yet but it looks good :)

Dave