2008-12-18

ASP.NET Development Server Responds with 403 - Forbidden in Firefox

I've been working on an ASP.NET site for a few months that requires Windows Authentication, and it was working fine when running the site in the ASP.NET Development Server and accessing in Firefox.

That is, until today.

Today, it decided to start kicking back Forbidden messages every time I tried to access it. Why would it start doing that to me?

It turns out I had at some point told Firefox to remember the password for the site, then had to change my domain credentials after doing so. Since my saved password didn't match the new password, the site was forced to return a Forbidden. I would hope that Firefox would prompt me for a new password when the authentication failed, but it didn't.

So how do we get back to good in Firefox?
You have to go into the Saved Passwords settings in Firefox and remove the site from the list of passwords to store.

In Firefox 3:
Tools | Options
Click on the "Security" button on the ribbon at the top of the Options dialog
Firefox Security Options dialog
Click the "Saved Passwords..." button in the "Passwords" section of the "Security" tab.
Find the offending site in the "Saved Passwords" dialog and click the "Remove" button.
Firefox Saved Passwords dialog
Click "Close" on the "Saved Passwords" dialog.
Click "OK" on the Options dialog.
Open the site again and Firefox should prompt you for the new credentials.

Hope this helps!

2008-11-14

Why We Must Use Classes Instead of Structs for Read/Write DataBinding in C#

Structs are Value Types.
Classes are Reference Types.

When we DataBind in C#, if we're using Value Types... we get copies of those objects instead of pointers to them, for that's how they are passed to the bound control. Therefore, you can't edit the bound control and expect the original objects to be modified. The bound control is only editing the copies and thus we encounter side effects when DataBinding a list or array of value types (like structs.)

I ran into this on a project I was working on where I needed to show a DataGridView for editing attributes. My attribute representation was a struct with a name property and value property... and when I bound a BindingList to the DataGridView, the list would appear... but when I'd edit the values would return to the old values... and when I'd add rows, the values would disappear.

The explanation above explains why. It didn't dawn on me until I started searching for "BindingList structs" and found Bill Wagner's "Of DataBinding and Value Types" (archive.org since the site is no longer available). The title tipped me off because I wasn't thinking about the fact that structs are value types.

Bill's article explains in more eloquent detail than I care to go into here, as I don't feel the need to re-write a well written essay on the topic. I only hope to provide a quick answer for those searching for it, that don't find Bill's article first (and leave myself a quick reference when I forget the next time I try to use a struct...)

He also mentions in his article when it is appropriate to use structs. Thanks for a well-done article, Bill.

So, reminder: use classes instead of structs when you need to do read/write binding. If you're merely databinding for a viewable list... use structs as you like.

2008-10-18

ASP.NET - Getting the Virtual Application Root Path for Your Site

HttpContext.Current.Request.ApplicationPath

I had to blog this here, because I have the need for it every once in a while, but not frequent enough to remember it... :)

Examples:
A site hosted without a virtual directory: mysite.com/
Returns: "/"

A site hosted from a virtual directory: mysite.com/myVirtualDir
Returns: "/myVirtualDir"

See the MSDN Documentation for more details and code examples.
See the "ASP.NET Web Site Paths" article for more ways to discover paths.

2008-10-06

Make Visual Studio Prompt You For Debug Source Code File After Cancelling the First Time

I've run into this problem a few times where I've been given a copy of a dll from another development team that has a PDB file with it... but they forget to send me the source code...

While debugging, I get the prompt for the source code file the PDB has signalled the debugger for (since it doesn't know where to find it initially...) and of course, since I don't have it, I click Cancel.

When you click cancel at this prompt, it adds to a repository in the solution of files NOT to search for. This is annoying, because after that first prompt, you ask the team for the source code so you can go back, expecting the prompt again, and step into the potentially problematic code.

Well, Visual Studio supresses the prompt because of this list within the solution of files not to search for.

"How do I get this back?!" you ask...

Here's the "solution" (get it?!):

The 'Do not search for these source files' dialog

Right-click your solution in Solution Explorer and go to Properties.
Then click on the "Debug Source Files" section under Common Properties.
At the bottom-right of the dialog, there is a "Do not look for these source files:" section. There lies the last-known path to the source-code file (which I assume is provided by the PDB.)
Delete that sucker and Apply/OK out.

Debug again and when you get to the point you want to step into that code again, you will get the prompt and life will be happy again (well, happier, until you fix your bug.)

Happy coding...

2008-09-02

How to Make Visual Studio 2008 Continue After an Unhandled Exception

I was debugging an app that was not handling a particular exception; and I found that after the Exception Assistant dialog appeared and I hit F5 to continue, that it would repeatedly recall the offending line of code which would again throw the exception.

I searched the net and found this forum thread on MSDN that shed some light on how to allow the program to continue after the exception is thrown.

Tools >> Options >> Debugging >> General
(General options show if you only go to Debugging)

Uncheck the "Unwind the call stack on unhandled exceptions" to prevent the debugger from reverting to the offending line as the next statement to call.

2008-08-27

Compatibility Tag for Internet Explorer 8

Microsoft emailed me today to warn me that Internet Explorer (IE) 8 will now be in "Standards Mode" by default instead of the "Compatibility" (sometimes referred to as "Quirks") mode the older versions have run in.

This means that web developers that may have sites out there with invalid markup may run into some issues for people viewing their sites using IE 8.

This is a GOOD thing Microsoft is doing, as it will make their browser play more like the others that are in "Standards" mode by default.

The email Microsoft sent me is to inform developers that they're not left in the dark. Microsoft has offered a way to make the sites tell IE to go back into "Compatibility" mode so it will look the same as it would in IE 7 or earlier.

NOTE: This is not an excuse not to fix your markup!!! You still need to fix your sites to be compliant with standards... this will just buy you some time until you do.

Go to Microsoft's article to find out how to use this compatibility tag should you need to.

Make sure to download Internet Explorer 8 beta and see if your sites may need this tag... your site will render differently in IE7 and IE8 if you do; if they look the same, you're a good developer! :)

2008-08-23

All Your Undelete Are Belong To Us


Click to Enlarge

My friend Clint was trying to do some merging between branches in Microsoft's Team Foundation Server (TFS) the other day, when he came across the error shown in the picture above:

"TF14087: Cannot undelete '[TFS FileName Here]' because not all of the deletion is being undeleted."

Ummmm, ok...?! :)

2008-08-14

JavaScript Won't Run on Internet Explorer as a Local File When Copied From Another Computer

My friend Walt was trying to write a little HTML page with JavaScript to automate a task at work for himself and some colleagues.

He developed it on his own computer; saved it and ran it in Internet Explorer (the only browser allowed to be on their computers at work.) On his computer, the page appeared in "My Computer" (counted in the "Internet" zone if you double-click to bring up the security settings.) Internet Explorer prompted him to allow the ActiveX content.

He then put the file on a network share and allowed his colleagues to download it to their own local machines.

When they downloaded and opened the file in Internet Explorer on their local computers, it appeared as "Restricted Sites" zone. This confused both of us, so I asked him to send me the file.

He transferred it to me via Windows Live Messenger and I saved it to my desktop. I got the same result when I opened it in Internet Explorer.

For an experiment, I edited the file... copied all of its contents into a new file I created from scratch on my machine. When I loaded the new file in Internet Explorer, it appeared as "My Computer" in the "Internet" zone as it had when Walt originally created it.

Thus we can deduce that IE must look at the file's properties and determine if a file was created on the machine asking to run it and if not, locks it down as a "Restricted Sites" file. It treats the ones created on the machine in question as "Internet."

While annoying, this is a nice security feature... and now you know how to get around it...
1. Make sure you analyze the code before you copy to a new file - if you don't and you introduce malicious code on your own machine... shame on you and Microsoft did all they could to protect you. Shame on you... :)
2. Copy the code into a new file that you save to your own machine.
3. Open in Internet Explorer

Of course, I don't know that Firefox has the same restriction, because it doesn't allow direct calls via JavaScript to ActiveX objects so it may run just fine without the extra steps... If so, and you have Firefox on your computer; well, you know what to do.

2008-08-08

Visual Studio Hotfix for Slow Context Menu in Solution Explorer for Solutions with 100+ Projects in Them

Microsoft released a Hotfix for those that have to work with solutions with over 100 projects in them (I feel your pain.)

Their disclaimer warns that it hasn't gone through their full testing process, so you're on your own with it... but we've had good luck with it so far using a solution that has over 150.

Enjoy.

PowerCommands for Visual Studio 2008

For those of you that have to work in Solutions that have tons of projects in them in Visual Studio 2008; you've probably become quickly annoyed by the fact that when you open the solution, all of the projects are expanded.

I got tired of it and searched around the net for something that would help... I found PowerCommands for Visual Studio 2008 by klevy. You can right-click anywhere in the Solution Explorer and select "Collapse Projects" from that level.

It has other really cool features too like:
  • Clear all panes (like Error List, Output, etc.)
  • Copy Path (puts the path of the file in Solution Explorer in your clipboard)
  • Copy and Paste References
  • Edit Project File (to hand-edit the XML)
  • And much more!

I highly recommend it.

2008-05-06

Bug Discovered in Internet Explorer 7 JavaScript Regular Expression Engine

I enhanced my understanding about Regular Expressions' "look-ahead" concept that I hadn't fully comprehended before. The fact that (at least in the .NET framework and most JavaScript engines; I assume it also works in PERL and other popular RegEx engines...) you can use look-aheads to not only find a match but not include in the full-text match; but can look-ahead and not have the position of the start of the match matter either.

For some reason I always thought that the match would be made and the beginning of the next match would start at the position the other left off at. I wet my pants with glee when I found that wasn't the case...

That is, until I tried an expression in JavaScript in Internet Explorer 7.

To see an example of what I'm talking about, run my "Regular Expression Tests for Your Browser" on JavaScriptJedi.com in multiple browsers on your computer. You'll be running the "Pre Match Look Aheads (2008-05-05)" tests which are the only ones there at the time of this writing. I may add more in the future.

The scenario is this:
I had a set of password restriction requirements to enforce on a form when a user wanted to change their password. The requirements were that the password must be at least 8 characters long; contains at least one number and at least one "symbol". Due to a database constraint, I also added a max-length requirement of 50 characters, and I interpreted "symbol" to mean any non-alpha-numeric character.

That said, the regular expression ended up like this:
/^(?=.*[0-9])(?=.*[^a-zA-Z0-9]).{8,50}$/

What's really cool about this is; when run in a properly working regular expression engine, it doesn't matter if the number or "symbol" come first, it will detect them both and then from the beginning of the string match any characters between the lengths 8 and 50. Genius. Whoever came up with Regular Expressions needs a Nobel prize or some equivalent; because, well, they just make a developer's world a better place to live. :)

So after getting uber-excited about my new found knowledge and greater love for Regular Expressions, my bubble was quickly burst when I found out it didn't work correctly in IE7 and I had to come up with a workaround.

So go see my page and try it for yourself; and I'd love to get your feedback on this one. I'll be submitting a bug report to Microsoft (if I can figure out how...) and if I hear anything back from them (which I'm sure I won't...) I'll post my findings here.

I'd especially like to hear from Mac users that can test on the major Mac browsers for me and tell me which work and which don't.

2008-05-04

MbUnit: Syntax Sugar for the Test-Driven Developer

I'm not going to profess to be the utlimate TDD guru; but I'm a firm believer and have been trying to continuosly better my skills in this development methodology.

I started my TDD experience about three years ago using CSUnit; but whas quickly moved to NUnit when we found that CSUnit wasn't totally meeting our needs. I've been with NUnit ever since and have heavily used it over the past year in NUnit GUI, NCover and MSBuild. It's easy to use and gets the job done well. The Unit Runner is a little lacking in some areas (mostly aesthetics... but we're not going for prize winning eye-candy... we're testing our code...) but it's functional and easy-to-use.

A friend of mine that works for LanDesk clued me in to one I hadn't heard before called MbUnit. I started searching around and realized that a lot of big names in the community seem to be excited about it. One blog post that really caught my eye and piqued my interest in checking it out was by
Scott Hanselman, entitled: 'MbUnit - Unit Testing on Crack'.

How wonderful it looked to write one short test for multiple scenarios rather than multiple test methods with basically the same repeated code over and over again with different test data.

So I took the simple phone number example I like to use when introducing someone to test-driven development and I made it my first experiment with MbUnit.

Without going into TOO much detail, I basically will have my phone number scenario be where we're going to write a utility class with a static method on it that will take a phone number string (in this case USA phone numbers) and return a boolean specifying its validity as a USA phone number format.

I make it a "red-green-refactor" scenario by first writing tests and implementing code that has pretty hard defined logic like string length tests, etc and then once I get some basic tests passing, I refactor the method's code to a regular expression and ensure the tests still pass. It's a fun little scenario and easy to help people pick up on the concept of Test-Driven Development.

In this scenario, the requirements were that the phone number had to be 10 digits and could only follow the formats (# representing a digit):
##########
###-###-####
###.###.####
(###) ###-####

Anyway, I was very pleased with the Test Fixture and Test code and how nicely the Icarus test runner still views them as separate tests.

The Test Code:


[TestFixture]
public class PhoneNumberIsValid_Tests
{
[RowTest]
// Success cases
[Row(PhoneNumberType.USA, "8888675309", true)]
[Row(PhoneNumberType.USA, "888-867-5309", true)]
[Row(PhoneNumberType.USA, "888.867.5309", true)]
[Row(PhoneNumberType.USA, "(888) 867-5309", true)]
// Failure cases
[Row(PhoneNumberType.USA, "(888.867-5309", false)]
[Row(PhoneNumberType.USA, "888.EYE.ROCK", false)]
[Row(PhoneNumberType.USA, "888EYEROCK", false)]
[Row(PhoneNumberType.USA, "888/867/5309", false)]
[Row(PhoneNumberType.USA, "123456", false)]
[Row(PhoneNumberType.USA, "123456789", false)]
[Row(PhoneNumberType.USA, "12345678901", false)]
public void USPhone_Tests( PhoneNumberType whichType, string testPhoneNumber, bool expectedResult )
{
bool result = PhoneNumberUtil.IsValid(whichType, testPhoneNumber);
Assert.AreEqual(expectedResult, result);
}
}


How cool is that to have your test method take parameters so the same test code can be used for multiple scenarios? Booya!

And you can see that I could easily add tests for non-USA phone number types by adding Row attributes that used a different value of the PhoneNumberType enum.

And here's how it looks inside Icarus:



Notice how it also shows the parameters being sent in to each test... makes it VERY easy to see what's going on with your tests. I love it!

Oh yes, MbUnit is Test-Driven Development evolved. I'm very excited about the potential of this framework.

Icarus Test Unit Runner is VERY alpha right now, but I'm excited at its potential. It's prettier than NUnit's GUI, though still showing its infancy. Icarus is very functional for being in alpha. Rumor is that ReSharper can also run MbUnit tests; so that will be awesome if that's true as well.

So, if you're a test-driven developer and you're looking for some new hotness in test-driven technology... I'd highly recommend looking into MbUnit.

To get hooked up with MbUnit and the Icarus test unit runner (and more) download the Gallio Automation Platform. Enjoy!