Created
January 24, 2011 05:21
-
-
Save chrisnicola/792875 to your computer and use it in GitHub Desktop.
A sample authentication interceptor for Nancy using Castle Interceptors for AOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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