Custom Runtime Policies - Glimpse/Glimpse GitHub Wiki
Runtime policies are what dictate how Glimpse responds to any given HTTP request. The runtime policy is controlled by a collection of IRuntimePolicy implementations.
The Glimpse and Glimpse.AspNet packages ship with several runtime policies (source available on GitHub) that define the common behavior of Glimpse:
-
AjaxPolicy- Ajax Policy disables Glimpse from changing to the response body for AJAX requests. -
LocalPolicy- Local Policy forces Glimpse to ignore requests from remote clients. -
ContentTypePolicy- Content Types filter what specific Content Types Glimpse will be enabled for. -
UriPolicy- Blacklist URIs filter Glimpse to only work on URIs that match specifc regex. -
StatusCodePolicy- Status Codes filter what specific Status Codes Glimpse will be enabled for.
Policies can be configured in web.config.
The Glimpse.AspNet NuGet package adds a stub IRuntimePolicy implementation to a user's web application. The stub file, once uncommented, looks like this:
using Glimpse.AspNet.Extensions;
using Glimpse.Core.Extensibility;
namespace Users.Web.Application.Namespace
{
public class GlimpseSecurityPolicy:IRuntimePolicy
{
public RuntimePolicy Execute(IRuntimePolicyContext policyContext)
{
var httpContext = policyContext.GetHttpContext();
if (!httpContext.User.IsInRole("Administrator"))
return RuntimePolicy.Off;
return RuntimePolicy.On;
}
public RuntimeEvent ExecuteOn
{
get { return RuntimeEvent.EndRequest; }
}
}
}
The stub shows an example of a policy that only allows Glimpse to run for users in the "Administrator" role. Role based security, as well as IP address and user agents, are common criteria for custom policy.
Note: To display Glimpse data to only those in the Administrator on a remote server, you must disable local only policy in the web.config file with the following markup:
<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
<runtimePolicies>
<ignoredTypes>
<add type="Glimpse.AspNet.Policy.LocalPolicy, Glimpse.AspNet"/>
</ignoredTypes>
</runtimePolicies>
</glimpse>
Glimpse allows for several runtime policies to be configured at any given time, but will only operate under the lowest policy returned by the collection.
##More Examples
Here are some more examples of whats possible with creating different policies,