Skip to content

Instantly share code, notes, and snippets.

@chrisnicola
Created January 24, 2011 05:21
Show Gist options
  • Save chrisnicola/792875 to your computer and use it in GitHub Desktop.
Save chrisnicola/792875 to your computer and use it in GitHub Desktop.
A sample authentication interceptor for Nancy using Castle Interceptors for AOP
public class AuthenticationInterceptor : IInterceptor
{
bool CanIntercept(IInvocation invocation) { return invocation.Method.GetCustomAttributes(true).Any(a => a is RequiresAuthenticationAttribute); }
public void Intercept(IInvocation invocation)
{
if (!CanIntercept(invocation)) invocation.Proceed();
else
{
var module = invocation.InvocationTarget as NancyModule;
try
{
if (module.Request.Query.api_key == "Password")
invocation.Proceed();
else
invocation.ReturnValue = new Response { StatusCode = HttpStatusCode.Unauthorized };
}
catch (RuntimeBinderException)
{
invocation.ReturnValue = new Response { StatusCode = HttpStatusCode.Unauthorized };
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment