Cryptographically Random Unique String

Looking for a way to create a Random Password Generator, I came across the following code written by Peter Bromberg (I thought to post it here in case it saves someone some time). This code will generate a cryptographically random unique string of any length you want.

Ah! BTW, I’ve made a very small modification to the original code

using System.Security.Cryptography;
using System.Text;
 
namespace UniqueKey
{
    public class KeyGenerator
    {
 
        public string GetUniqueKey()
        {
 
            int maxSize = 8;
            char[] chars = new char[62];
 
            chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
            byte[] data = new byte[1];
 
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);
            data = new byte[maxSize];
            crypto.GetNonZeroBytes(data);
            StringBuilder result = new StringBuilder(maxSize);
 
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length - 1)]);
            }
 
            return result.ToString();
        }
    }
}

Thanks Peter for the code!

I’m still here

I haven’t had the time to write more tips because I’ve been working on a big project which is almost complete.

As soon as I can, I wanna share with you guys some tips/tutorials on how to do more complex SQL queries like combining different JOINs and such. I’ll be using MySQL for this. Later, I’ll put examples using MSSQL.

Later! :D

System.Web.HttpException: The file ‘/path/to/userControl.ascx’ does not exist.

If you’re working with User Controls (or other server side controls) and your file structure looks something like this:

[] = directory

[root]
     [userControls]
          ucTest.ascx
     .....
     ucContainer.aspx
     Web.config

And in your ucContainer.aspx file you have something similar to this:

< %@ Register Src="userControls/ucTest.ascx" TagName="UserControl" TagPrefix="uc1" %>
...
...
<uc1:UserControl id="ucUserControl" runat="server" />
...
...

And from your code behind, you are loading the control like this:

ucUserControl.LoadControl("userControls/ucTest.ascx");

It will compile with no problems, BUT at run time it will give you this nasty error message:

Exception Details: System.Web.HttpException: The file '/path/to/ucTest.ascx' does not exist.

This happens because when you load your control, you need to do it like this:

ucUserControl.LoadControl("~/userControls/userControl.ascx");

Also, make sure you add the tilde (~) character in your .aspx Register directive, Like so:

< %@ Register Src="~/userControls/ucTest.ascx" TagName="UserControl" TagPrefix="uc1" %>

The ~ (tilde) character is the root path reference syntax which lets you add a reference to your controls, pages, etc. without having to hard code relative paths into your URLs like so:

ucUserControl.LoadControl("../../userControls/userControl.ascx");

Using this syntax will let you move your controls to other sub directories (if you ever need to) without having to worry about going back to your ucContainer.aspx.sc file and change the ‘hard coded’ path to the UC’s new location.

Of course, if the .ascx file and its container reside in the same directory, you don’t need to include the tilde (~) character into your URL’s path.

Hope this helps :)

Just Upgraded to Version 2.1

WordPress 2.1 Ella has been released a few hours ago. I just upgraded my blog to this new version and it’s just awesome. These are some of its new features:

  • Autosave makes sure you never lose a post again.
  • Our new tabbed editor allows you to switch between WYSIWYG and code editing instantly while writing a post.
  • The lossless XML import and export makes it easy for you to move your content between WordPress blogs.
  • Our completely redone visual editor also now includes spell checking.

You can read more on their release page.

The guys at WordPress.org rock!

Parser Error Message: Could not load type ‘_Default’

While developing a .Net web app in C# with Visual Studio 2005, I got this error:

Parser Error Message: Could not load type ‘_Default’

The reason: My website configuration was using the wrong .NET framework version (1.1.4322).

The solution: Change it to use a .NET 2.0 version. In my case, 2.0.50727

The result: a nicely displayed page :)

If you’re experiencing something similar, I hope this sheds some light ;)

Disabling the PHPSESSID in the Query String

If you’ve worked with PHP sessions, probably your URL looked something like this:

http://www.yourdomain.com/page.php?PHPSESSID=59aa95ad46cd67d82ba0f812407326dd

If you don’t like the PHPSSID being displayed in the query string, you might have wondered how to disable it.

Well, a way to disable it is by setting session.use_trans_sid to off in your php.ini. However, if you don’t have access to the php.ini file because you’re hosting your site on a shared server, then, you can accomplish this by using a .htaccess file*.

The following example worked for me:

<IfModule mod_php4.c>
     php_flag session.use_trans_sid off
</IfModule>

That’s it! I hope this helps.
––––

*Make sure your hosting provider allows .htaccess files.

/usr/bin/wget: Permission denied

If you’ve ever got the error /bin/sh: /usr/bin/wget: Permission denied when trying to execute a .php script using a cron job, spent minutes/hours trying to find a solution to this, well, I hope this post will help you to save some time.

It’s possible that your hosting provider, for security reasons, is not allowing you to execute the wget command. If you want to use the wget (or GET) command, you can do two things:

a) send an email to your hosting provider’s support department. Or

b) you can try using the following command:

php -q /path/to/script.php

The above example worked for me nicely. However, if it still doesn’t work for you, then contact your hosting provider; maybe there are other things that they need to take care of (or fix) first before you can work with cron jobs.

Cheers!