Adventures In ASP.NET

Tips, tricks, and frustrations from my years of building ASP.NET websites.

Monday, April 21, 2008

List the groups for the specified user in C# using ADSI.

Using the ADSI IADsUser class and it's 'Groups' method to get a list of groups of which the user is a member is fairly simple once you know the trick.

First, you will have to add a reference to the 'Active Directory DS' COM object.
Second, add the statement: using ActiveDS;

Here is a GroupNames() function to demo the process:

using ActiveDS;

public static string[] GroupNames(string userName)
{
   List result = new List(20);
   if (String.IsNullOrEmpty(userName) == true)
   {
      return null;
   }


   userName = userName.Trim().Replace('\\', '/');


   DirectoryEntry de = new DirectoryEntry(String.Format("WinNT://{0},user", userName));


   IADsMembers groupsForUser = (IADsMembers) de.Invoke("Groups",null);


   foreach (object group in groupsForUser)
   {
      DirectoryEntry deGroup = new DirectoryEntry(group);
      result.Add(deGroup.Name);
   }


   if (result.Count == 0)
   {
      return null;
   }
   
   return result.ToArray();
}

Thursday, April 10, 2008

Using the ADSI class IADsGroup in .NET

Want to call the ADSI function IADsGroup.IsMember(x) in .NET. There is an easy way to do this but the solution is not well known. Check out my function IsMember(user,group):


public bool IsMember(string aUser, string aGroup)
{
  if ((String.IsNullOrEmpty(aUser) == true) || (String.IsNullOrEmpty(aGroup) == true))
  {
  return false;
  }


  aUser = aUser.Trim().Replace('\\', '/');
  aGroup = aGroup.Trim().Replace('\\', '/');


  System.DirectoryServices.DirectoryEntry de = new System.DirectoryServices.DirectoryEntry(String.Format("WinNT://{0},group", aGroup));


  object[] obj = new object[1];
  obj[0] = String.Format("WinNT://{0}", aUser);


  // Call the IADsGroup.IsMember() function.
  return (bool)de.Invoke("IsMember", obj);
}

Wednesday, April 9, 2008

Invalid Viewstate errors when hosting on a Web Farm

Everybody has seen the dreaded 'Invalid Viewstate' error from time to time. There are many reasons this can occur. If you host your site on multiple hosts behind a content switch on a web farm you will immediately receive these errors in bulk.

There is an easy solution to this problem:

1. Generate 2 unique HEX keys using this program.

2. Change the \Windows\Microsoft.NET\Framework\<.NET version>\CONFIG\machine.config file on each host in your web farm and add this entry (use your generated keys):


<system.web>

<machineKey validationKey="thekey" decryptionKey="thekey" validation="SHA1"/>

</system.web>

Sunday, April 6, 2008

Anonymous FTP in ASP.NET the easy way

There is an easy way to do an anonymous FTP in ASP.NET. The trick is to use the WebClient class to perform the operation.

The function below will perform the FTP and return the StreamReader to read the newly downloaded file:


using System;
using System.Security.Permissions;
using System.Net;
using System.IO;


System.IO.StreamReader AnonFTP(string theFileToDownload, string theLocalFile)
{
try
{
WebClient request = new WebClient();

// FTP site uses anonymous logon.
request.Credentials =
new NetworkCredential("anonymous","YourEmail@YourHost.com");

request.DownloadFile(theFileToDownload,theLocalFile);
return new System.IO.StreamReader(theLocalFile);
}
catch
{
return null;
}
}



Sample Invocation:

AnonFTP("ftp://x.com/dat.txt", "C:\\test.txt");

Saturday, April 5, 2008

Adding an 'Are You Sure' confirmation box to a GridView delete button.

When deleting a row in an ASP.NET GridView control, often times you would like to provide a confirmation dialog box with a simple message and the basic 'OK' and 'Cancel' buttons.

This isn't too hard to accomplish using:
1) Template column.
2) <asp:LinkButton> control and it's OnClientClick property.
3) A little JavaScript

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton
ID="lbtnGridDelete"
runat="server"
CausesValidation="false"
CommandName="Delete"
OnClientClick='<%# Eval("Role","return confirm(\"Are you sure you want to delete role: {0}?\")") %>'
Text="Delete">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
The trick is to use the OnClientClick property of the LinkButton control. OnClientClick is the property you use to specifiy the JavaScript to execute when the delete button is clicked. In this case we use the Eval() function to formulate our JavaScript and print our message on the confirm() box. The value of the column named 'Role' will be placed in the message, for example: Are you sure you want to delete role: asdf?

The resulting confirm box will look like this:



When the users presses the delete button, the client side code will run to produce the JavaScript confirm() box. If the user presses the 'OK' button then the post back will occur and the delete processing will execute.

About Me

I'm currently employed as a Senior Systems Programmer/Analyst and sometimes Project Manager at Texas Instruments Inc. in Plano TX.