Craig Gibbons' Lifeblog lifeblog://tri.eat.net

22Nov/050

ASP.NET error when impersonating a user

Lately I have been developing a small application which is essentially the same as Windows Explorer. The idea is that business units within the company I work for, can publish documentation easily by giving a single user access to a given filepath, which I then impersonate when making calls to the file system in my application. Since I started developing the application, I have come up against numerous perplexing problems which have all had something to do with Windows or .NET security in one way or another. The latest one I came up against is as follows:

"Configuration information could not be read from the domain controller, either because the machine is unavailable, or access has been denied."

I blog it here because I was unable to find any useful information on this error anywhere and only managed to solve it by making a lucky (if educated) guess. The solution to this problem was to give the ASPNET account "Impersonate a client after authentication" rights on the machine the application is running on. For some reason, a reboot was not required in this case. You can find this setting by going to Control Panel -> Administrative Tools -> Local Security Policy, then expand Local Policies and select User Rights Assignment. Note that it will probably also be necessary to assign the "Act as part of the operating system" right to the ASPNET account, although you probably would not have gotten to this error before doing that in any case.

Filed under: Tech No Comments
11Nov/050

CommunityServer upgrade

For many months, I have been trying to upgrade gibbons.co.za to CommunityServer, the new .NET based blogging software from Telligent Systems. Each attempt has been met with blinding frustration and dismal failure, but at last, after trying and trying again, I have prevailed and managed a successful upgrade to CommunityServer v1.1 with the aid of two very key pieces of software. I write here as complete a description as possible of the procedure followed, tools used and problems encountered so as to provide assistance to any other would-be CommunityServer migrants. After running the new software for only a week, I can comprehensively say it is a vast improvement on .Text v0.95, although I intend no disrespect whatsoever to the original blog software, which was excellent for its time.

Migrating to CommunityServer is essentially a simple process, consisting of the following steps. Note that you must use CommunityServer v1.1 because the DotText-CS-Converter wizard (mentioned in point 5 below) only works with this version.

  1. Download the CommunityServer software from Telligent and follow the instructions in the README.
  2. Follow the setup instructions. Once complete you should have a functioning blog running in multi-blog (normal) mode.
  3. If you have an old .Text 0.95 blog, use Ken Harder’s DotText-CS-Converter tool to migrate the data. Full instructions on how to do this are included in the README accompanying the tool.
  4. To run your blog in single-blog mode, you need to replace the default SiteUrls.config in your site root, with one generated using Ken Robertson’s Single Blog/Gallery SiteUrls Generator. Note that you need to set the options ‘Single Blog’ and ‘Single Gallery’ to ‘Yes’. Your blog name will probably be 'blog_0'.
  5. You should then be able to open your site as per usual.

Troubleshooting

If you hit your site after following the above steps and the first page displays fine, but you get a 404 (Page not found) error when clicking any of the links, it’s probably because your site is not correctly configured in IIS to support URL rewriting. If you are hosting your site with a hosting company you may have a problem here. I had initially taken a hosting package with Fasthosts.co.uk but after much banging of head against the nearest wall, I finally contacted Ken Robertson and pleaded for help. He was gracious enough to advise me that the option “Check that file exists” must be unchecked. This option can be found by going into Internet Services Manager, then by choosing ‘Properties’ on the website, go to the ‘Directory’ tab and click ‘Configuration’, then select ‘.aspx’ from the ‘Application Mappings’ list on the ‘Mappings’ tab and click ‘Edit’. Fasthosts was unwilling to uncheck this box for me, so after a protracted discussion, I ended up getting flatly refused and am now hosting elsewhere.

I encountered one other problem which is worth mentioning. When running the DotText-CS-Converter wizard, I chose the option to migrate trackbacks. My site has been in operation for about a year now and I had close on 1000 trackbacks. Ordinarily this wouldn’t be a problem, but after migrating several hundred of those, I got an error saying the transaction log was full. I guess it’s obvious, but any hosting company will impose a size quote on your database and log to prevent disk space shortages. The number of transaction taking place during the migration quite simply used all that space.

Good luck.

Filed under: Tech No Comments
20Jul/050

My first article on CodeProject.com

For ages I’ve been meaning to share the odd pearl with the good readers of CodeProject.com but never seemed to find the time or perhaps more truthfully, code good enough, to post on the site. Finally though, I have posted my first article on CodeProject.com entitled "A useful UrlBuilder class". So far the readers are liking what they're seeing and that’s not only flattering but also quite encouraging. If I can find the time to write a whole article and make it look all pretty, there might be more articles to come. Hmmm, I have just the one in mind! You can find my article at the following Url: http://www.codeproject.com/aspnet/UrlBuilder.asp

UPDATE 06/12/2005: I wrote another article for CodeProject.com on August 24th, but didn't updated this post. You can find the new article at the following Url: http://www.codeproject.com/aspnet/EncodedUrlBuilder.asp

Filed under: Tech, Watch & Read No Comments
17Jun/050

A useful UrlBuilder class

This post has been fully revised and updated, so much so in fact, that I decided instead to write an article for CodeProject.com. You can find a full explanation of the UrlBuilder class and associated source code at: http://www.codeproject.com/aspnet/UrlBuilder.asp

Filed under: Tech No Comments
15Mar/050

Raising events from a Web User Control

One of the more advanced topics in ASP.NET is that of communication between a page and the controls it contains. There are various approaches to this problem and some methods work better in some circumstances than others. Sometimes however, it is necessary to take action in a page based on an action performed in a user control, such as a button being clicked, a dropdown posting back or any number of other possible events that can be raised. This sounds quite daunting at first, especially if you come from a non-OO background or otherwise are not used to working with events, but it really is very powerful and in fact not very difficult to master. Let's take a common scenario where a page contains a single Web User Control and we wish to respond to a button click in that control. The explanation of the code is quite involved, so the reader will have to cope with the following code example.

First off, we need to register the event in the user control. This is done in the same way you would declare any variable:

public event EventHandler ButtonClicked;

Then, we need a pair of functions to handle and then bubble the event to the container page:

protected void btnButton_Click(object sender, System.EventArgs e) {
OnButtonClicked(sender, e);
}

public void OnButtonClicked(object sender, EventArgs e) {
if(ButtonClicked != null) {
ButtonClicked(sender, e);
}
}

That’s all that needs to be done on the control side. Now all we have to do is write an event handler on the page and voila! We have raised and handled an event from a user control. You do this in the same way you write any event handler, just let the IntelliSense help you along. Yup, .NET rocks!

Filed under: Tech No Comments
3Mar/050

C# – DelimitedStringToInt32Array, a useful conversion routine

Today I was presented with a mini programming challenge, that being, to convert a delimited string to an integer array. While this is by no means tough, I thought I'd post the code here in case anybody is interested. This type of routine is quite useful when, for example, passing a list of ID's in a QueryString. Of course this code is made available without any implied warranties and without liability to the author, yours truly.

private int[] DelimitedStringToInt32Array(string DelimitedString, string Delimiter) {
if(DelimitedString.Length == 0) {
return new int[0];
}

try {
string[] sourceArray = DelimitedString.Split(Delimiter.ToCharArray());
int[] destinationArray = new int[sourceArray.Length];
for(int i = 0; i < sourceArray.Length; i++) {
destinationArray[i] = Convert.ToInt32(sourceArray[i]);
}

return destinationArray;
} catch(InvalidCastException ex) {
throw new InvalidCastException("At least one of the source elements could not be cast to the destination type.", ex);
}
}

Filed under: Tech No Comments
2Mar/050

iPod Mini 4Gb

I finally did it! Well, actually Laura H and I did it! Wondering down Tottenham Court road this past Saturday afternoon, chomping at the bit to spend some cash on a shopping spree which has thus far failed to materialise we passed one of those little tech stores with a very unassuming facade other than a board outside which said something to the effect of "Amazing prices on iPod mini's". Far be it from me to get caught up in sales hype (working at an online casino does that to you), but I figured what the hey, let's see what they're offering. We wondered in and the sales guy informed us they were letting them go for a mere £140, startling when you consider the price the first hit the high street stores at was a ridiculous £230! A few moments later, we had both bagged a pod in his and hers colours, blue for me and pink for Laura. Score! If you're in the market for a mini, now is a good time to buy the 4Gb model. Apple recently announced the launch of a new 6Gb model, with 18-hour battery life, as opposed to the 4Gb 12 hours; so pretty much any vendor will be dropping their prices soon, if they haven't already. The new model comes in around £170. The way I see it is, these things are disposable. Whatever you buy, whenever you buy, it will without question be obsolete in a few months and if it isn't, you'll still be longing for the latest and greatest kit. This is an essential survival item for the London urbanite. You can buy an iPod mini from Amazon.co.uk for (at the time of writing) £139.00

Filed under: Cool kit, Tech No Comments
17Feb/050

gibbons.co.za is about to get a face lift

3 months ago I started this blog after looking around for quite some time to find a suitable blogging application. I wanted something good, free and customizable. After quite a bit of shopping around and time wastage investigating red-herrings which were no longer, supported, developed or even used, I finally came across what I would consider to be the de facto .NET blog application, known as .Text (dot text). The project was originally started by a guy named Scott Watermasysk who devoted 2 years of his life and love to building .Text into what you see here on this site. At some point a company called Telligent Systems was started by a guy named Rob Howard and they quickly went about procuring the services of Scott Water and another guy named Jason Alexander, creator of nGallery. Telligent took the Microsoft sample application, ASP.NET Forums, .Text and nGallery and rolled these immensely popular applications into a single application called CommunityServer and as I write this I buzz with excitement because version 1.0 is expected to be released tomorrow. gibbons.co.za will be upgrading to this new version of CommunityServer as soon as a migration tool becomes available, after all I really don't want to be repopulating a database all night! CommunityServer is free for non-commercial use. More information can be found on the Telligent Systems website.

Filed under: Tech No Comments
2Feb/050

C# – Reading an Embedded Resource file

Sometimes we want to store some text inside an assembly without having to write it as part of the actual application code. A typical example of this is embedding some JavaScript which is then later read out and added to a page before rendering. In this way, an entire JavaScript library can be developed for a web application and parts included as and when required. The procedure is actually quite simple. First, add the file to the project, call it whatever you like, the important part is that the Build Action in the item properties (press F4 while the item is selected) must be set to 'Embedded Resource'. This means the resource will get compiled into the assembly along with all the code. Second, we need a small amount of code to read this embedded resource out at runtime. Some sample code follows:

using System.IO;
using System.Reflection;

public static string GetResource(string resourceName) {
Assembly assembly = Assembly.GetExecutingAssembly();
TextReader textReader = new StreamReader(assembly.GetManifestResourceStream(resourceName));
string result = textReader.ReadToEnd();
textReader.Close();

return result;
}

Note that the parameter 'resourceName' is the fully qualified path to whatever you called the file.

That's all there is to it! I always say this but it's always true, .NET Rocks!

Filed under: Tech No Comments
28Jan/050

C# – Removing HTML tags from a string using Regular Expresions

Regular expressions are largely misunderstood and shunned by the majority of developers, probably because they have their roots in PERL and to most Microsoft developers, all that *nix stuff is to be kept well away from. It will fry your monitor and crash your hard drive, it might even make your coffee taste bad, besides, who wants to use something some open-source tree-hugging hippy developed 20 years ago for the command line? The other notable barrier to entry is that VBScript only got regular expression support in version 5.0, JScript had it from the beginning so it's a little confusing that Microsoft didn't include it in VBScript from the outset, afterall, if they wanted to create a language to replace JScript, they should have implemented all the features of JScript and more. Truthfully, regular expressions are our friends, they are infinitely useful for an array of operations, which can actually all be accomplished by parsing text the old fashioned way, but which are alltogether more elegant using a regular expression. The syntax is a little difficult to grasp in the beginning but once understood, opens a treasure trove of possibilities to the learned grinning developer. Today, I was faced with a simple problem. I had to write something to remove all the HTML tags from a string. I've done this the old fashioned way some time ago and it worked well, but clearly regular expressions are the "right" way to go about solving this problem. I got out my old favourite regular expressions guide and had a quick look for a regular expression to do the job. Not finding one, I just wrote a basic one, which granted, may not account for every possibility, but which will work for just about 99% of all developers and all scenarios. The expression goes as follows:

Regex regex = new Regex("</?(.*)>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
htmlString = regex.Replace(htmlString, string.Empty);

The beauty of this approach, is that the desired result can be achieved in just 2 lines. Oh yeah, .NET Rocks!

Filed under: Tech No Comments