Posts

Showing posts from January, 2013

Impersonating CurrentUser from SYSTEM

Hey all, I've been searching for a very long time an easy way to impersonate current user while running in a "SYSTEM" user credentials. I haven't found any code that does it and actually works. So I managed to combine pieces of code from several places, and added some of my own. This helper class exposes 2 functions: 1. Get current user impersonated scope - in this scope you can do pretty much whatever you want in the current user credentials. 2. Start a process as current user. public static class ImpersonationUtils { private const int SW_SHOW = 5; private const int TOKEN_QUERY = 0x0008; private const int TOKEN_DUPLICATE = 0x0002; private const int TOKEN_ASSIGN_PRIMARY = 0x0001; private const int STARTF_USESHOWWINDOW = 0x00000001; private const int STARTF_FORCEONFEEDBACK = 0x00000040; private const int CREATE_UNICODE_ENVIRONMENT = 0x00000400; private const int TOKEN_IMPERSONATE = 0x0004; private const int TOKEN_

Json To Dictionary generic model binder

Hey all, Few days ago I was challenged by a good friend and colleague to write a generic model binder for a dynamic json. Why does he need such a thing ? I'll explain: Normally, when you write a classic website using any platform, the contract between the client and the server is obvious and well known. However, if you write a complex web system, and you have the need to build your client model dynamically without creating a massive model on the server that handles all kinds of properties and values, you might be interested in getting a generic dictionary that contains all your client values. To implement it I used a state machine to parse the json: public class DictionaryModelBinder : DefaultModelBinder { private const string _dateTimeFormat = "dd/MM/yyyy HH:mm:ss"; private enum StateMachine { NewSection, Key, Delimiter, Value, ValueArray } public override object BindModel(ControllerContext controllerC

Testing controller actions with ActionFilterAttribute in ASP .NET MVC

Hey, Have you ever wanted to test your controller actions with the logic you add with ActionFilterAttributes? Well, I did. So I wrote this helper class, which executes the ActionFilterAttribute methods. public class FilterAttributeTester { public ActionExecutingContext ExecuteOnActionExecuting<TAttribute>(Controller aController, Func<ActionResult> aAction) where TAttribute : ActionFilterAttribute { var actionExecutingContext = CreateActionExecutingContext(aController, aAction); var attribute = GetAttribute<TAttribute>(aAction); attribute.OnActionExecuting(actionExecutingContext); return actionExecutingContext; } public ActionExecutedContext ExecuteOnActionExecuted<TAttribute>(Controller aController, Func<ActionResult> aAction) where TAttribute : ActionFilterAttribute { var actionExecutedContext = CreateActionExecutedContext(aController, aAction); var attribute = GetAttri

Add Styles & Scripts Dynamically to head tag in ASP .NET MVC

Hey, I wanted to be able to create partial views in asp .net mvc in a way that every partial will declare its own styles and scripts. So the naive solution is to define a section in the layout, and in the partial view just put everything inside. The problem is, it's working only for one level deep. What happens if your partial view uses another partial views ? What happens if your layout is dynamic, and uses partial views as well? So I created the following solution: public class DynamicHeaderHandler : ActionFilterAttribute { private static readonly byte[] _headClosingTagBuffer = Encoding.UTF8.GetBytes("</head>"); private static readonly byte[] _placeholderBuffer = Encoding.UTF8.GetBytes(DynamicHeader.SectionPlaceholder); public override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.Result is ViewResult) { filterContext.HttpContext.Response.Filter = new ResponseFilter(filterContext