Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Utility classes designed for ASP.NET MVC; deals with encryption, routing, caching, authorization, and various other security issues. Designed to used with Dependency Injection in ASP.NET MVC3.
branch: master
Failed to load latest commit information.
.nuget added Nuget package restore
MVC.Utilities.Azure Implemented bulk get behavior for AppFabric Cache
MVC.Utilities.BCrypt Made it so all projects in MVC.Utilities pull their version informati…
MVC.Utilities.Memcached.Tests Made it so all projects in MVC.Utilities pull their version informati…
MVC.Utilities.Memcached Added a default constructor for MemcachedService
MVC.Utilities.Tests Renamed the RuntimeCacheProvider to RuntimeCacheService; also provide…
MVC.Utilities Renamed the RuntimeCacheProvider to RuntimeCacheService; also provide…
buildscripts updated the package dependency information in the Rakefile for MVC.Ut…
tools added all of the unit runners to the solution
.gitignore fixed some issues with our output tasks (precursors for nuget packaging)
Gemfile added a Gemfile and fixed a lot of issues with the Rakefile
Gemfile.lock added a Gemfile and fixed a lot of issues with the Rakefile
MVC.Utilities.1.1.dotCover Added dotCover file
MVC.Utilities.sln added Nuget package restore
README.textile Fixed a punctuation error in README.textfile
SharedAssemblyInfo.cs version 1.13.5
VERSION version 1.13.5
license.txt Initial commit. Patches welcome!
mvc-utilities-azure.nuspec Updated assembly info and nuget packages
mvc-utilities-bcrypt.nuspec Updated assembly info and nuget packages
mvc-utilities.nuspec Updated package to 1.08
package.cmd Upgraded package.cmd to suppor the NuGet 1.6 command line options
rakefile.rb version 1.13.4

README.textile

MVC.Utilities

This is a batch of utility classes for making it easier to work with ASP.NET MVC.

Authentication

Create a new FormsAuthenticationService instance thusly, or inject it via an IoC container:

var authenticator = new FormsAuthenticationService();

You can sign-in users and set a persistent ASP.NET Forms authentication cookie:

authenticator.SignIn(UserName, true);

You can sign-out users again, which clears the authentication cookie:

authenticator.SignOut();

Caching

MVC.Utilities supports a couple of different caching options out of the box, all of which derive from the ICacheService interface and CacheServiceBase abstract base class.

The RuntimeCacheService uses the System.Runtime.Caching namespace to take advantage of built-in object caching – this is cache is simply an abstracted, more testable version of ASP.NET’s built-in object caching.

Create a new RuntimeCacheService instance – specify the type of ObjectCache you want to use and a default expiration window:

var cacheService = new RuntimeCacheProvider(MemoryCache.Default, new TimeSpan(0, 0, 20));

Use the Save, Exists, Get, and Delete methods to act on objects in dictionary maintained by the cache. These commands will all work as expected for the AppFabricCacheService as well for those of you using Windows Azure

Encryption

MVC.Utilities has two out-of-the-box encrpytion providers which both derive from the ICryptoService interface: HMACSHA1Service and BCryptService.

To hash a password, simply call the HashPassword method:

var hash = _crypto.HashPassword(originalPass);

To check to see if two passwords are equivalent, use the CheckPassword method:

var areEquivalent = _crypto.CheckPassword(originalPass, hash);

Routing

MVC.Utilities contains a set of helper classes designed to tackle some specific routing challenges in ASP.NET MVC, particularly where search engine optimization is concerned.

Lowercase Routes

Courtesy of Nick Bernardi, we have the LowerCaseRoute class in MVC.Utilities.

Here’s an example of how to use it inside of Global.asax:

routes.Add(“default”, new LowerCaseRoute(“{controller}/{action}/{id}”,
new RouteValueDictionary(
new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }),
new MvcRouteHandler()));

Search Engine-Friendly Slugs

For sites that serve user-generated content, some developers make it a priority to give those pages SEO-friendly URLs.

The SlugHelper utility class helps make this easy:

//slug will look like ‘complex-looking-name’ (without quotes obviously)
var slug = SlugHelper.GenerateSlug(“Complex Looking Name!!!!”);

Security

Currently there’s only one additional security-related class in MVC.Utilities.Security: the UploadedImageValidator.

The UploadedImageValidator verifies that a user-uploaded image is an image in JPEG, GIF, or PNG format and (optionally) is under a specified file size.

var imageStream = ImageContainer.AsStream("test.jpeg");
var isValidImage = UploadedImageValidator.FileIsWebFriendlyImage(Stream stream);

UI and Display Helpers

The DateTimeLocalizationHelper provides a ghetto, server-side method for localizing times by simply displaying all times as relative times ala Hacker News.

For example, 3/3/2011 2:11 will read as “2 hours ago” if you’re looking at the content at 3/3/2011 4:14.

Simply call the ToRelativeDateTime method from your standard HTML helper class.

@Html.ToRelativeDateTime(Model.DatePosted)

Install MVC.Utilities via NuGet

If you want to install MVC.Utilities via NuGet, simply type this command into the package manage console in Visual Studio:

Install-Package mvc-utilities

If you want access to the Azure-specific helpers in MVC.Utilities, install this package also:

Install-Package mvc-utilities-azure
Something went wrong with that request. Please try again.