Use Craig's Utility Libray in Webmatrix

Jan 10, 2013 at 6:07 PM

I'm using Webmatrix.

I installed Craig's Utility Library via Nuget. I now have a Utilities.dll in my Bin folder.

So far so good, I guess, but how do I actually use or invoke a utility in this library, in particular Randomization?

Thank you.

 

Coordinator
Jan 10, 2013 at 8:12 PM

To be honest, I've never used Webmatrix so I'm not 100% sure at present. Give me a day or two and I can take a look (basically I'll install it and give it a try tonight). That said, it sort of depends on the section as to what you would want to do. In terms of randomization, they're mostly extension methods built off of System.Random. So, in MVC/C# (which I do most of my work in lately), you would simply add this to the controller:

using Utilities.Random.ExtensionMethods;

Then create a System.Random object.

Random MyRandomGenerator=new Random(MySeedNumber);

At this point you can use the Next, Shuffle, and NextEnum extension methods. In order to randomly generate a class, you would create your class and mark the properties with the various attributes in the Utilities.Random namespace (StringGenerators contains string specific attributes, NameGenerators contain name specific attributes, DefaultClasses contains integer, float, etc. attributes, and ContactInfoGenerators contains contact info specific attributes):

public class MyClass
{
   [NameGenerator]
   public string Name{get;set;}
   [IntGenerator(1,99)]
   public int Age{get;set;}
}

With that you could make the following call which would generate a random object of MyClass:

MyClass Result=MyRandomGenerator.NextClass<MyClass>();

Jan 10, 2013 at 9:54 PM

Thank you that would be great.

What I want to do is create a random salt string.

Coordinator
Jan 11, 2013 at 12:22 AM
Edited Jan 11, 2013 at 12:23 AM

If that's what you're trying to do, then what you actually want is to use a function like this:

 

private static string CreateSalt(int size)
{
    //Generate a cryptographic random number.
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    byte[] buff = new byte[size];
    rng.GetBytes(buff);

    // Return a Base64 string representation of the random number.
    return Convert.ToBase64String(buff);
}

Which I got from here: http://stackoverflow.com/questions/2138429/hash-and-salt-passwords-in-c-sharp. But basically you want to use the RNGCryptoServiceProvider class which is in the System.Security.Cryptography namespace. It creates a strong salt where as my random functions would not. Mine are more if you were looking to fill a database with random values for testing, etc.

Jan 11, 2013 at 5:57 PM
> Thanks for the function, I'll give it a try.
> I'm not sure it will work in Webmatrix but never know till one tries.
>
> If you are still up for checking to see if, and how, your Utilities work in Webmatrix I am still interested in several other of your utility functions.
> Thanks again, Jack.
>
> From: JaCraig
>
> If that's what you're trying to do, then what you actually want is to use a function like this:
> private static string CreateSalt(int size)
> {
> //Generate a cryptographic random number.
> RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
> byte[] buff = new byte[size];
> rng.GetBytes(buff);
>
> // Return a Base64 string representation of the random number.
> return Convert.ToBase64String(buff);
> }
> Which I got from here: http://stackoverflow.com/questions/2138429/hash-and-salt-passwords-in-c-sharp. But basically you want to use the RNGCryptoServiceProvider class which is in the System.Security.Cryptography namespace. It creates a strong salt where as my random functions would not. Mine are more if you were looking to fill a database with random values for testing, etc.
>
> Read the full discussion online.
Coordinator
Jan 11, 2013 at 7:41 PM

I'm still looking into it. I'm already seeing what I can do to turn at least a portion of the code to a portable library so adding webmatrix to the list of things to test would actually be pretty easy... Now I just need a night when family isn't telling me that I have 10 things that need to get done...

Coordinator
Jan 12, 2013 at 4:48 PM

By the looks of it, from the testing that I've done, it's about the same as if you were doing MVC. So you should just be able, for any of the extension methods, just add the appropriate using statement. For the other items, you should just be able to create the object. There are a couple namespaces that wont work (couldn't get LDAP to work for instance) but most items should work.

Jan 12, 2013 at 9:18 PM
Hi.
Thanks for checking this out for me, and avoiding the family's list of 10 things to be done.
Hope they are still talking to you.

So, I'm fairly new to Webmatrix such that I still struggle with some new or complex things.
I'm missing something very fundamental wrto these Utilities.
And I'm sure it will a "duh" when shown the solution.
Could you please show me the code for 2 of your utilities: FirstDayOfWeek and IsNull.

I have your Utilities.dll in my bin folder.
And have added @using Utilities to my code.
But I'm getting an error "Unknown" when I try to use either of these utilities.

Thanks so much.


From: JaCraig

By the looks of it, from the testing that I've done, it's about the same as if you were doing MVC. So you should just be able, for any of the extension methods, just add the appropriate using statement. For the other items, you should just be able to create the object. There are a couple namespaces that wont work (couldn't get LDAP to work for instance) but most items should work.
Coordinator
Jan 14, 2013 at 6:47 PM

Actually what you would want to do is add @using Utilities.DataTypes.ExtensionMethods (all of the extension methods are in one of the namespaces under Utilities and end in ExtensionMethods). For FirstDayOfWeek you would do:

DateTime.Now.FirstDayOfWeek()

And for IsNull, for any object that you want to check it would just be:

MyObject.IsNull()

Jan 14, 2013 at 8:10 PM
Awesome, the rest of the @using is what I was missing.
Thanks!
At least I had 2 out of 3 right (I had the FirstDay and IsNull nailed) so thx for the confirmation.

Comment for other WebMatrix users - the System.Security.Cryptography seems to be not available in the Webmatrix environment.

Thanks again for all your great help!
And the Utilities.


From: JaCraig

Actually what you would want to do is add @using Utilities.DataTypes.ExtensionMethods (all of the extension methods are in one of the namespaces under Utilities and end in ExtensionMethods). For FirstDayOfWeek you would do:

DateTime.Now.FirstDayOfWeek()

And for IsNull, for any object that you want to check it would just be:

MyObject.IsNull()