/mvc-utilities

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.

Primary LanguageC#OtherNOASSERTION

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