Tuesday, December 31, 2013

Disabling ASP LinkButton controls in newer versions of Internet Explorer

Since IE 10, I have noticed that when a LinkButton control is disabled (Enabled="False"), the rendered link is not grayed out like it used to be and seemingly should be.  The link is disabled for all intents and purposes, but just appears to be enabled.  This can cause confusion to users if you rely on this mechanism.  The quick fix has always been "compatibility view" in IE, but this is being phased out over time by Microsoft as we get newer version of Internet Explorer.

Interestingly enough, the html rendered is the same.  You still get an anchor tag with the disabled attribute set to "disabled".  It is the browser that is reacting differently to this tag.  IE no longer styles by default an anchor tag with this particular attribute the way you expect (i.e. "grayed out").  You are forced to provide the css to render in this scenario.  The following css should style your LinkButton like the good ole days.
    a[disabled="disabled"] {
        color: gray;
        text-decoration: none;
    }
    a[disabled="disabled"]:hover {
        color: gray;
        text-decoration: none;
    }
Now, maybe you can finally turn off compatibility view!

Thursday, December 26, 2013

Debug .NET web applications without timing out

This happens when you are hosting the web application in IIS.  Just open up IIS, click on the application pool and go to Advanced Settings.  Set Ping Enabled to False.  Apparently IIS pings itself occasionally and if it doesn't get a response, then it shuts down.  Keep in mind that while you are debugging, IIS is blocking all other requests, so it sees this as a bad thing.  So turning this behavior off during development is fine.

Friday, December 20, 2013

Manage file extensions served by a web server

If your web site exposes files for users to open, and certain file types open and certain file types don't, then you will need to add the extensions that aren't working to the MIME Types associated with the site in IIS.

  1. Open up IIS
  2. Select the Site, and choose MIME Types in the IIS section.
  3. Click on Add, and add the extension.

A quick google search should obtain the MIME Type for the extension you want to add.

Simple and secure exception handling for .NET web applications

It is best practice not to display details of an exception on the page visible to anyone.  To hide these details, simply add the following to your web.config.
<configuration>
  <system.web>
    <customErrors mode="On" defaultRedirect="WebPages/ErrorPage.aspx"></customErrors>
  </system.web>
</configuration>
You can also change the mode attribute to "RemoteOnly" to only hide errors if viewing the site on a machine other than the web server.  The redirect simply takes the users to a page other than the "yellow screen of death" page that ASP provides.

It is also imperative to add a Global.asax file to your web application project.  You'll want to log anything that fires off the Application_Error event since these represent any unhandled exceptions and the details will no longer be visible on the screen.  Something like this will accomplish that:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when an error occurs

    Dim MyException As Exception = Server.GetLastError()

    ' Log the exception details here.
End Sub

Tuesday, December 17, 2013

Show or Hide the login window when accessing a site that uses Windows Authentication.

You're working on a site that is set up for Windows Authentication.  Two scenarios:

  1. You want your credentials to "pass through" without logging in.  In IE, open Internet Options, Security tab.  Select the zone the site is in, or add it to a zone if appropriate.  Click the Custom Level button.  Scroll down and select the "Automatic logon with current username and password" under the User Authentication, Logon section.
  2. You don't want to be logged in automatically so you can log in as other people for testing or whatever.  Follow the steps above except choose the "Prompt for username and password" radio button.

Wednesday, December 11, 2013

Implementing Windows Authentication

Windows authentication essentially means you are letting users into your web site if they have a valid AD user id and password in a specified group.  It is simple and works great for an intranet application.  It's important to note that IIS is responsible for the actual authentication.  So first, you need to enable Windows Authentication for the site.  To accomplish this:
  1. Open IIS.
  2. Select the site.
  3. Click "Authentication" in the IIS group.
  4. Disable "Anonymous Authentication"
  5. Enable "Windows Authentication"
It's possible that Windows Authentication has not been installed on the web server.  If this is the case, you will need to enable that feature through the control panel.

At this point, when a user browses to your site, the browser will see that anonymous authentication is disabled and will display a login box because of the windows authentication.  Once, the user authenticates, IIS sends the user information to the actual web application.  At this point, it is up to the web application to authorize the user.  

This authorization is configured in your web.config.  Here is an example that restricts access to users in a certain AD group:
<configuration>
    <system.web>
        <authentication mode="Windows" />
        <authorization>
            <allow roles="PowerUsers" />
            <deny users="*" />
        </authorization>
    </system.web>
</configuration>
Remember that the allow element takes precedence of the deny element.

To get the user name on an aspx page, simply type User.Identity.Name.  To get the user name in .net server side code, use the fully qualified System.Web.HttpContext.Current.User.Identity.Name.

Wednesday, December 4, 2013

Eclipse "intellisense"

Intellisense is actually a Microsoft term for autocompletion functionality in Visual Studio while writing code.  However, Eclipse offers the same feature for Java and Android development.  It's called method selection helper or something like that.  Anyway, it pops up automatically by default but disappears after you select something or type over what it suggests.  To get it back, just use the shortcut key combination of ctrl-space.