Friday, June 27, 2008

ASP.NET tactics: Saving files to network shares

Getting a web app to talk to the local file system is hard enough. Getting it to talk to (and certainly to write files) to a networked system is even more difficult.

The initial problem is that the security context that the site runs as is a local one. To complicate things, the security context changes as you progress through the life of an ASP.NET web request. The IIS service typically runs as "Network Service". However, you can set a particular site or virtual directory to run under another user. Furthermore, you can configure ASP.NET to impersonate yet another user. So it can be very difficult to figure out exactly "who" it is that is doing some action against the file system.

As I mentioned, the user context for these processes are local ones. In order to successfully access a network share you'll need to run your site and possibly process(es) as domain accounts in order to get security contexts between machines that will cooperate. Of course, this then also requires both machines being members of the same or trusted domains.

One solution may be to create a virtual directory in the site you are trying to do this in. That virtual directory will point to the network location you wish to save files to. When you set it up you can specify what user to connect as, and in this case you can specify an external user context, such as a local account from the other machine (MACHINENAME\USER). This would likely be a bit more secure that modifying the IIS sites or process. When you save files, you'll just save them to a "local" virtual path such as /mysite/myMappedVirtualDir.

Unfortunately, setting up things like this typically becomes trial-and-error. You have to just keep trying different things until it works. It is understandable that web site technologies adhere to the principle of least privilege, but it often gets in the way of simply getting things done.

Monday, June 23, 2008

Networking a Windows 98 virtual machine

Yes, sadly, I had to create a VirtualPC instance of Windows 98.

I'm getting into a project for an app that must support Windows 98, if you can believe that. Fortunately in this day and age I can run 98 through virtualization, and it runs very fast on the modern hardware.

I installed the OS without incident. However, TCP/IP networking just didn't seem to want to work. I didn't get any errors regarding hardware or drivers. The OS found a network adapter called "Intel 21140 based 10/100 mbps Ethernet Controller" that it liked and had a driver for. I had everything set properly to work on the network I am on. It just wouldn't acquire an address.

I googled a bit and found a forum post that wasn't terribly useful but led me to looking at the Virtual PC settings screen for the virtual machine. Under the "Networking" setting, you can choose the number of adapters and what each adapter is. It seems the default is "Nortel IPSECSHM Adapter" which Windows 98 doesn't seem to like much.

Another adapter choice was "Intel(R) PRO/1000 PL Network Connection" which is the same name of the adapter on the physical machine (or at least the name that shows in the Virtual PC host OS, Windows XP in my case). So I switched it to that and started the virtual machine. Presto Chango! It started right up and the network adapter got an IP address from the DHCP server.

Friday, June 06, 2008

Productivity with key cords

You can assign keyboard short-cuts to start menu items in Windows. Visual Studio allows you to map every known command to keyboard shortcuts. This can be a major help for those programs and commands that you use often. My philosophy is that the keyboard (and thus the keys) are always in the same place (very helpful when you have multiple monitors and a lot of ground to cover with a mouse cursor). Even when mousing, my left hand is always on the keyboard so many of my most frequently used shortcuts are left-hand-only. It's amazing how easy it is and how much time it can save.

Below are my favorites. Each is preceded with an indicator of where the shortcut is defined ([W]dows or [V]isual [S]tudio). Some of the Visual Studio cords are more applicable for web application development.

W: Ctrl-Alt-S
Launch source control. (Probably only useful for you if you use a explorer type utility for you particular source control system such as VSS or Vault.)

W: Ctrl-Alt-Shift-S
Launch SQL Server management studio.

W: Ctrl-Alt-D
Launch Windows command shell.

W: Ctrl-Alt-Shift-P
Launch Windows PowerShell.

W: Ctrl-Alt-F
Launch Firefox.

W: Ctrl-Alt-C
Launch Beyond Compare, my favorite comparison tool.

W: Ctrl-Alt-E
Launch Internet Explorer.

W: Ctrl-Alt-X
Launch Microsoft Excel.

W: Ctrl-Alt-M
Launch mail app (Outlook in my case).

W: Ctrl-Alt-V
Launch Visual Studio 2005.

W: Ctrl-Alt-Shift-V
Launch Visual Studio 2008.

W: Ctrl-Alt-O
Launch Opera.

W: Ctrl-Alt-P
Launch PasswordSafe (password management tool).

VS: Ctrl-Alt-Shift-A
Attach to processes. I use this to manually attach to running processes instead of always launching apps using "Run" which starts the browser and attaches to the server and browser. Usually I just run web apps in an active browser and attach as needed. 9 out of 10 times the problems in the code are obvious and I don't need to actually step through code. Plus, when you have a web app that loads up session variables, requires logins, etc. it's far easier to just dive into debugging then to have to re-launch and go thru all the steps to reproduce a problem.

VS: Ctrl-Alt-Shift-D
Detach from all processes.

VS: Ctrl-Alt-B
Build solution

VS: F6
Build the active project. Very handy for when you are fixing simple compile time errors. If I have a project with many errors that I'm working on, I will quite often just rebuild with this instead of hunting for the collapsed error list as I work my way through the errors.

VS: Ctrl-Alt-B (VS default shortcut)
Build solution

VS: Ctrl-Alt-Shift-B
Re-build solution. For me, this one does require two hands. But I use it less often as "Build solution" and my right hand is usually on the keyboard already.

VS: Ctrl-Alt-Shift-C
Show differences between working file and source controlled copy. (This VS command only works when the focus is on the solution explorer). However, that is only a matter of the next shortcut.

VS: Ctrl-Alt-L
Move focus to the solution explorer. You can move back and forth between the solution explorer and the active code editing window with this and ESC.

VS: Ctrl-Alt-Shift-H
Show file history in source control.

VS: Ctrl-Shift-F4
Close all documents.

The one catch with using shortcuts is that you have to avoid overlap. You can't set up a short cut in a particular application that is the same as one used in Windows, as Windows will catch it first. That's why I have some commands modified with the Shift key, such as the process detach short cut that would otherwise conflict with launching a command shell.

Aside from these, I also use some of the standard built in Windows short-cuts:
WinKey-E: Windows Explorer
WinKey-R: Start -> Run
WinKey-M: Minimize all windows
Ctrl-Shift-ESC: Task manager

Overall, once you start using keyboard shortcuts you'll find it far easier and faster to execute tasks. People often comment on how fast I am when working. It's not so much that I'm faster than them, I just use the apps and tools in a more efficient way. We all have enough work to do, there's no reason to make it harder by wasting time hunting for programs and commands that we use all the times.