Saturday, November 14, 2009

ASP.NET Data Controls

I found an excellent article that highlighted the similarities and differences among ASP.NET Data Controls:

Understanding the Differences Among the DataGrid, DataList, and Repeater

I've used the DataGrid and Repeater in the past (never really used the DataList), but more often than not, I'd lose track of the subtle differences between ASP.NET data controls.

The biggest problem in my view is that the name really does not provide a clear picture of either the usage or the rendered HTML of the particular Data Control.

There's also the ListView (and its DataPager) that was introduced in 3.5. I've not had a chance to write actual code using ListView ... hoping to write at least some sample code, but I've been so busy cramming for the ASP.NET exams that I haven't been doing much hands-on .NET Development.

It's ironic, but I've found that most certification exams require a high-level understanding of a vast array of topics, and even when they occasionally delve in-depth, one is better served by reading quickly through a large book (like my Microsoft Press book) than writing an application that utilizes certain technologies (such as writing a Photo Gallery that uses ListView, Membership Providers, etc.)

Saturday, November 7, 2009

ASP.NET Page Lifecycle events

From my ASP.NET experience, understanding the Page Lifecycle and what occurs at each point is one of the most confusing (and important) aspects of ASP.NET development.

Here are the 2 MSDN pages that I've gone over several times and memorized:

There is also several pages in my Microsoft Press book that are very similar to these MSDN pages.

The challenge is remembering each of the events, and what is appropriate to do at that particular event. For example:

  • PreInit - Set Master Page, Set Theme, create dynamic controls
  • Init - Controls have been initialized, theme skins applied. Initialize control properties.
  • InitComplete - Perform tasks that require initialization has been completed.
  • PreLoad - Perform any processing that should occur before Load.
  • Load - Set properties in controls and establish database connections
  • Control Events (calling of delegates based on events raised, should check Page.IsValid if there are any Validator controls).
  • LoadComplete - Perform tasks that require load has been completed.
  • PreRender - Prior to this event occurring, Page calls EnsureChildControls for all controls, including itself. Also, all data bound controls have DataBind method called.
  • SaveStateComplete - ViewState has been encoded and saved, then this event occurs.
  • Render - Page object calls Render on every control.
  • Unload - event occurs for each Control, finally for the Page. Do cleanup, close database connections.
*sigh* I like ASP.NET, but I've tended to only use a few of these Lifecycle events (PreInit and Load) to keep things simple. They are SO granular, as you can see from the list above, and it's difficult to remember what-goes-where.

Wednesday, November 4, 2009

Microsoft Certification Studies: 70-562

Next up ... Exam 70-562: Microsoft .NET Framework 3.5, ASP.NET Application Development

The starting point for ASP.NET is the System.Web.UI namespace.

I've spent a lot of time on the MSDN site for this namespace, having done a lot of ASP.NET development work. I've practically lived on the API for Page and Control, and sites that document the ASP.NET Page Lifecycle, so this is more review and refamiliarization than anything.

However, I am sure there will be some new material I haven't been exposed to yet.

I'm also using the Microsoft Press book "Microsoft .NET Framework 3.5 - ASP.NET Application Development" by Mike Snell, Glenn Johnson, Tony Northrup, and GrandMasters.

Read through the first 2 chapters this evening.

Passed Microsoft Exam: 70-536

I passed my Microsoft .NET 70-536 exam today. Woo-hoo!

My primary approach to preparing for this exam was the Microsoft Press book that covered the exam, as well as just pouring over the MSDN FCL documentation.

I also wrote a lot of little test programs (some of which I ended up posting on my blog). I've done a lot of .NET development, but the Skills Measured (from Microsoft's site) covered a lot of aspects of the .NET Framework that I've never used (nor had a reason to use).

The biggest ones being:

- COM / Interop. I've done standalone .NET Windows Forms clients, installer programs, and a Globalized ASP.NET Website. Never had a reason / need to interface with COM. I just studied the book on this, read articles on sites like CodeProject.

- System.Diagnostics. Coming from the Java world, I had always used log4j for my logging needs, so log4net seemed like a good, practical solution. Consequently, I never needed to use Systems.Diagnostics.

This evening, after work, I immediately began studying for 70-562: ASP.NET Application Development.

It feels like being in college again, taking final exams. :-) Good times!

Tuesday, November 3, 2009

Systems Diagnostics

For some reason, this is one of those topics I've really dreaded about the exam. I guess it's because most of my .NET Development has been done on ASP.NET, using log4net for my logging.

Consequently, I've tended to stay away from the Systems.Diagnostic (the stuff that writes to the Windows Event Log).

Ah, well, this is a good time to learn it, since the exam covers it.

Here is a test program I wrote to use the System.Diagnostic.EventLog, as well as System.Diagnostic.Debug.


using System;
using System.Diagnostics;

namespace Diagnostics
{
class Program
{
static void Main(string[] args)
{
String applicationEventSource = "Test Diagnostic Application";
if (!EventLog.SourceExists(applicationEventSource))
{
EventLog.CreateEventSource(applicationEventSource, "Application");
}

// Use static method to write an entry. Default EventLogEntryType is Information
EventLog.WriteEntry(applicationEventSource, "Hello, world!");

// Try using instance-specific methods to write a test error
EventLog eventLog = new EventLog("Application");
eventLog.Source = applicationEventSource;
eventLog.WriteEntry("Test error", EventLogEntryType.Error);

// Test debug
Debug.Listeners.Add(new ConsoleTraceListener());
String testString = "Company";
Debug.WriteLine("Test");
Debug.WriteLineIf(testString != "My Company", "Incorrect Company");
Console.ReadKey();
}
}
}

Sunday, November 1, 2009

Cryptography

I've been playing with the System.Security.Cryptography namespace, making sure I'm familiar with the classes here.

I'm really starting to feel good about taking the .NET certification exam 70-536, think I'll do it next week. I've been studying hours every night, and going over the Microsoft Press study book by Tony Northrup, as well as just reading the MSDN .NET FCL.

Wrote the following test program that shows how to create an MD5 hash.


using System;
using System.Security.Cryptography;
using System.Text;


namespace SecurityTest
{
class Program
{
static void Main(string[] args)
{
String unencryptedPassword = "password";
String encryptedPassword = EncryptPassword("MD5", unencryptedPassword);
Console.WriteLine("Encrypted password is: '" + encryptedPassword + "'");
Console.ReadKey();
}

public static String EncryptPassword(String cryptAlgorithm, String unencryptedPassword)
{
HashAlgorithm hashAlgorithm = HashAlgorithm.Create(cryptAlgorithm);

byte[] hash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(unencryptedPassword));

StringBuilder s = new StringBuilder();
foreach (byte b in hash)
{
s.Append(b.ToString("x2").ToLower());
}
string password = s.ToString();

return password;
}
}
}

Saturday, October 31, 2009

Authentication and Authorization

Well, it's Halloween, and in between going to answer the doorbell, I'm still studying for my Microsoft .NET 70-536 exam.

Can't think of anything I'd rather be doing on Halloween than studying and writing code. :-)

Authentication and Authorization classes reside in the System.Security.Principal namespace.

The following classes and interfaces exist in this namespace:

  • WindowsIdentity - Represents a Windows or AD user account.
  • WindowsPrincipal - Provides access to a user's group memberships. From my study book, "You can use the WindowsPrincipal class to determine which groups a user is a member of. To query for built-in groups, pass to the WindowsPrincipal.IsInRole method a member of the System.Security.Principal.WindowsBuiltInRole".
  • GenericIdentity - For simple, non-Microsoft directory service user account.
  • GenericPrincipal - For simple, non-Microsoft directory service groups.
  • IIdentity - For custom users.
  • IPrincipal - For custom groups.
  • WindowsBuiltInRole (enum) - local Windows groups that are common in NT, 2000, XP. Include: User, PowerUser, Administrator, Guest, AccountOperator, SystemOperator, PrintOperator, BackupOperator, and Replicator.



Here is sample code to get the current user's group memberships:


using System;
using System.Threading;
using System.Security.Principal;

namespace IdentityPrincipal
{
class Program
{
static void Main(string[] args)
{
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
Console.WriteLine("Current User: '{0}", currentIdentity.Name);

// Two ways to get the currentPrincipal.
// 1. Through the WindowsPrincipal constructor passing in currentIdentity.
//WindowsPrincipal currentPrincipal = new WindowsPrincipal(currentIdentity);

// 2. Through the Thread.CurrentPrincipal property (after setting the Principal Policy of the
// current domain. For this sample program, we will use this method.
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal currentPrincipal = (WindowsPrincipal) Thread.CurrentPrincipal;

if (currentPrincipal.IsInRole(WindowsBuiltInRole.User))
{
Console.WriteLine("Current User is a User");
}
if (currentPrincipal.IsInRole(WindowsBuiltInRole.Guest))
{
Console.WriteLine("Current User is a Guest");
}
if (currentPrincipal.IsInRole(WindowsBuiltInRole.PowerUser))
{
Console.WriteLine("Current User is a Power User");
}
if (currentPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
{
Console.WriteLine("Current User is an Administrator");
}
Console.ReadKey();
}
}
}