Archive

Author Archive

Using C# to Generate Random String with Character Filter

December 29th, 2010 2 comments

I haven’t posted anything for awhile, and today while toying around with a few different things I decided to tweak my random string generator.  So I thought I’d share.

So here we have two methods; one for Unicode and one for ASCII. My thought was that when generating a simple password for US English keyboards, I’d just use a simple ASCII generator using characters available on the keyboard. For for the rest of you folks who don’t have a US English keyboard, maybe there are some options to generate a random string with a custom range filter. 

public static string GetRandomUnicodeString(int length, int maxValue, Predicate<int> valueFilter)
{
    byte[] seedBuff = new byte[4];
    byte[] charBuff;

    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    rng.GetBytes(seedBuff); // The array is now filled with cryptographically strong random bytes.

    using (MemoryStream ms = new MemoryStream())
    {
        using (StreamWriter sw = new StreamWriter(ms, new UTF8Encoding(false, false)))
        {
            var random = new Random(BitConverter.ToInt32(seedBuff, 0));

            for (int i = 0; i < length; i++)
            {
                int temp = random.Next(maxValue); //should we cap? 

                while (!valueFilter(temp))
                    temp = random.Next(maxValue);

                sw.Write((char)temp);
            }
        }
        charBuff = ms.ToArray();
    }
    return new System.Text.UTF8Encoding(false, false).GetString(charBuff);
}

public static string GetRandomASCIIString(int length)
{
    return GetRandomUnicodeString(length, 0x7E, o => o >= 0x21 && o <= 0x7E);
    //return GetRandomUnicodeString(length, 126, o => o >= 33 && o <= 126); //could use integers instead
}

So when using the Unicode method, you have to specify a max value; otherwise, the random generator will choke if you want a small range of numbers when randomizing a pool of 65,535 values. So the cap will help performance. The filter expression should filter out the random results so you only get the characters you want.

The ASCII method is a pretty simple example of how this works.  I want a max value of 126, and I want characters in the range of 33 through 126. I used hex (because the Windows character map uses hex).

Hope you all find it useful.  Let me know how it goes.

Categories: .NET Tags: , , , , ,

Mono 2.8.1 Install Script for Ubuntu and Fedora Web Server

December 6th, 2010 14 comments

I just updated the install script for Mono 2.8.1. Better late than never. ;)  This script will install mono, gtk, gdi, xsp and mod_mono for use with a web server.  

Ubuntu:

wget --no-check-certificate https://github.com/nathanb/iws-snippets/raw/master/mono-install-scripts/ubuntu/install_mono-2.8.1.sh

Fedora:

wget --no-check-certificate https://github.com/nathanb/iws-snippets/raw/master/mono-install-scripts/fedora/install_mono-2.8.1.sh

 

After download:

chmod 755 install_mono-2.8.1.sh
./install_mono-2.8.1.sh

 

If you have previously installed Mono from source, you may want to uninstall it before deleting its source. Run sudo make uninstall from each component directory you previously installed.

Categories: Mono Tags: , , , ,

Setting up Mono 2.8 with Asp.Net 4.0 and MVC2 on Ubuntu with MySql Membership

November 23rd, 2010 30 comments

I’ve had a few requests to write a full walk-through installing Mono 2.8 w/ Asp.Net 4.0 since I posted an install script for Mono 2.8 on Ubuntu and Fedora.  So here it is! We’re going to be setting up a new server from scratch then installing just the basics of Mono 2.8 (Mono, GTK, GDI, XSP, & Mod_Mono). Then we’ll use that to setup a new Apache server with a basic Asp.Net 4.0 MVC2 application w/ MySql Membership.  That sounds like a lot, but it’s really very straight forward. So lets get started!

Read more…

Categories: ASP.Net, Mono, Ubuntu Tags: , , , , ,

Mono 2.8 Install Script for Ubuntu & Fedora

October 8th, 2010 60 comments

I updated both my scripts for Ubuntu and Fedora to download and install the latest 2.8 Mono release.  I’ve also been playing with GTK lately (specifically Pixbufs) and so this script now includes the install for GTK-Sharp. Anyway, you can find the updated scripts on my GitHub snippets project.  Both Fedora and Ubuntu install scripts should succesfully install mod_mono, Xsp, mono core, GTK, and GDIplus at a minimum.

If you have problems running your apps that use the gdi-plus, it could be resolved with a refresh of the DllMap.  Enter "sudo ldconfig"  and then try to re-run your app. Checkout http://www.mono-project.com/DllNotFoundException for more details.

2/20/2011 Update

Mono 2.10 has been released. I added a new install script here. 

1/27/2011 update

Mono 2.8.2 was released awhile back, which included some security fixes and enhancements. I have since posted an updated script along another blog post related to it here. 

10/26/2010 update

The script now installs to /opt/mono-2.8 instead of /usr/local in favor of the parallel mono environment suggestion. You may need to add /opt/mono-2.8/bin to your system PATH.

For Ubuntu, this is located in /etc/environment; insert /opt/mono-2.8/bin to the beginning of the path. Keep in mind, this will change the global PATH, so if your'e using a workstation, this may affect other applications that depend on Mono. Consider changing your profile PATH. (See comments for more details). 

For RH/Fedora, I updated the install script to also create an environment script at: /etc/profile.d to include this mono install to your system path.

Disclaimer: You should only try this script if your'e familiar with the process of compiling packages manually. I'm trying to make it work for MonoDevelop and other files, but it may not work with them in its current state.

Feel free to fork and make changes of your own to the script from GitHub. I'll gladly welcome pull requests containing improvements.

 

Read more…

Categories: Fedora, Mono, Ubuntu Tags: , , , , , ,

Mono 2.8 Released!

October 7th, 2010 No comments

Wow, I haven't seen much written about it other than the updated pages on the mono-project.com website and some tweets from Miguel de lcaza, but finally Mono 2.8 released! This release has some major updates include full C# 4.0 spec, MVC2 (official), and Asp.Net 4.0 mod_mono among a ton of other fixes and enhancements.  I'll be writing another blog post today with an updated script to compile/install the 2.8 build on Debian & RedHat distros. 

If you have any interest in running your .NET applications with Mono, now is a good time to try it out. With this release, you'll find very little resistance with incompatibilities for .NET 2.0-3.5. You still use Visual Studio for builds, and you can even take advantage of a huge extension framework built-in to mono. 
Categories: Mono Tags: , ,

Setting up SubSonic3 Repository Mode with an Existing Database

October 6th, 2010 No comments

Using SubSonic 3 respository mode has been a breath of fresh air for some of my projects. You can use simple POCOs (plain old CLR objects) and easily use inversion of control with your application. However, it can be a little confusing using repository mode with an existing database since SubSonic’s default schema naming may not conform to your database. By using SubSonic’s schema attributes, you can get your application up and running relatively easily; and if you have a ton of tables, I have a T4 template that generates basic domain classes against your database to get you started.

Read more…

Categories: SubSonic Tags: , , ,

Install Mono Daily Build Script for Fedora 13

September 8th, 2010 No comments

Mono ProjectFedora 13 A while back I added a Fedora 13 install script for Mono that downloads, builds, and installs the last tested daily build along with xsp and mod_mono.

I just recently updated it to use the Sept 1st, 2010 daily build. You can pull the latest script directly from my website or from my repository on github.

 

Script Usage

My test environment was a standard web server install from the Fedora 13 download DVD.  After the install, I started the same process I performed with the Ubuntu install, which was something like this:

mkdir mono
cd mono
wget http://www.integratedwebsystems.com/resources/p796/fedora_mono-trunk.sh
chmod 744 fed*.sh
./fedora_mono-trunk.sh

That’s it. It’ll first install all the required dependencies for the build and the web server. It’ll then download the 9/1 build (or latest tested version) from the daily tarball list and pull mod_mono and xsp from subversion trunk. Finally, it will extract, compile, and install everything.

The only thing remaining is to setup a web application and configure apache to use mod_mono. For this, I like to use the Mod Mono Config Tool as a starting point to build my virtual host configuration file.

Setup your web server:

  1. Move the mod_mono.conf from /etc/httpd/conf to /etc/httpd/conf.d
  2. Create a new directory to hold your virtual host config files at: /etc/httpd/sites-enabled

    mkdir /etc/httpd/sites-enabled
  3. Copy your new virtual host config file to /etc/httpd/sites-enabled
  4. Append a virtual host config line to your httpd.conf

    echo ”Include sites-enabled/*.*” >> /etc/httpd/conf/httpd.conf
  5. Disable Selinux by editing the /etc/selinux/config file and changing SELINUX=disabled
  6. Reboot server
  7. Start web server if it isn’t configured to auto-start. (use chkconfig to set auto-start)

    service httpd start

That should do it. You can test your new virtual host by browsing to it. The 404 response should show the new mod-mono version.

Good luck!

Categories: Fedora, Mono Tags: , , , , ,

SubSonic3 Works on Mono!

August 8th, 2010 2 comments

SubSonic  ProjectSo a couple weeks ago I decided to test a few changes made to SubSonic3 that fixed a LINQ evaluation bug related to medium trust. Using a simple console app on linux/mono, I tested basic data access to a SQLite database using repository mode, and to my surprise it worked!

Mono-gorilla-aqua.100px

I’ve always been a big fan of SubSonic since its 2.0 version, which I used for a mid-sized e-commerce site. With a totally rewritten core, 3.0 uses LINQ (language integrated query) for all of its data access providing a great alternative to Dblinq for the Mono platform.

 

 

Read more…

Cross-domain JsonP using Asp.net MVC and jQuery

July 24th, 2010 22 comments

So there are a couple great walkthroughs out there that talk about using jquery jsonp in cross-domain scenarios.  They talk a lot about the requesting and server side, but not so much about how to use the callback. So to clarify, I’ll step through what I did to make this work including providing my implementation for a new ActionResult called JsonPResult for MVC.

Read more…

Categories: ASP.Net, jQuery Tags: , , ,

MVC2 on Mono – Tips and Tricks for Windows Developers

July 20th, 2010 9 comments

So lately, I’ve been pretty wrapped up with kids and family so I haven’t had much time to post anything new.  However, I did start a new project, which I’ll detail out more as I go. It’s been quite the experience so far since I decided to build this one from the ground up using MySql and Linux/Mono.  I’ve never designed apps like that before. For one, I’ve had to learn a new set of design tools for MySql as opposed to my comfortable Sql Management Studio. And I’ve also had to very incrementally test different programming technologies and methods that were questionably supported in Linux/Mono.  The beauty of this is that no matter what I do to make it work for Mono, the app will always work on Windows, and there’s little development time lost if I have to fall back to its native platform.

Read more…

Categories: ASP.Net, Mono Tags: , ,