Skip to content

Instantly share code, notes, and snippets.

View EfrainReyes's full-sized avatar

Efrain Reyes EfrainReyes

View GitHub Profile
@EfrainReyes
EfrainReyes / MvcRegistry.cs
Created November 28, 2014 03:20
With this StructureMap Registry you can inject static and global objects like HttpContext and Session into your MVC controllers and any other helper classes you may have. Taken from Matt Honeycutt's pluralsight course on Building your own Application Framework
public class MvcRegistry : Registry
{
public MvcRegistry() {
For<BundleCollection>().Use(BundleTable.Bundles);
For<RouteCollection>().Use(RouteTable.Routes);
For<IIdentity>().Use(() => HttpContext.Current.User.Identity);
For<HttpSessionStateBase>()
.Use(() => new HttpSessionStateWrapper(HttpContext.Current.Session));
For<HttpContextBase>()
.Use(() => new HttpContextWrapper(HttpContext.Current));
public class ModelStateValidationAttribute : ActionFilterAttribute, IExceptionFilter
{
public override void OnActionExecuting(ActionExecutingContext filterContext) {
if(filterContext.HttpContext.Request.IsAjaxRequest())
return;
var modelIsValid = filterContext.Controller.ViewData.ModelState.IsValid;
if(!modelIsValid) {
filterContext.Result = new ViewResult() {
@EfrainReyes
EfrainReyes / ValidateAjaxAttribute.cs
Last active October 23, 2015 19:28
Use MVC's model state on Ajax forms, provided that there's some client-side code to capture the validation output
/// <summary>
/// http://stackoverflow.com/questions/14005773/use-asp-net-mvc-validation-with-jquery-ajax
/// </summary>
public class ValidateAjaxAttribute : ActionFilterAttribute, IExceptionFilter
{
public override void OnActionExecuting(ActionExecutingContext filterContext) {
if(!filterContext.HttpContext.Request.IsAjaxRequest()) {
return;
}
public class FormsAuthenticationService : IFormsAuthenticationService
{
private readonly HttpContextBase _context;
public FormsAuthenticationService(HttpContextBase context) {
_context = context;
}
public void SignIn(string userName, bool createPersistentCookie, IEnumerable<string> roles = null) {
string roleList = string.Empty;
public class CurrentUser : ICurrentUser {
private readonly IIdentity _identity;
private readonly HttpSessionStateBase _session;
private readonly UserInfoService _service;
private IUserInfo _user;
public CurrentUser(IIdentity identity, HttpSessionStateBase session, UserInfoService service) {
_identity = identity;
_session = session;
using StringHelpers;
using NUnit.Framework;
namespace olakease.Tests {
[TestFixture]
public class SuperTest {
[Test]
public void NullObjectShouldNotThrowException() {
object obj = null;