PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/System.Web.Http/Filters/AuthorizationFilterAttribute.cs

http://aspnetwebstack.codeplex.com
C# | 49 lines | 43 code | 5 blank | 1 comment | 6 complexity | c4b7ffca6c28c7c73cee89300b23fe72 MD5 | raw file
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Net.Http;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using System.Web.Http.Controllers;
  7. namespace System.Web.Http.Filters
  8. {
  9. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
  10. public abstract class AuthorizationFilterAttribute : FilterAttribute, IAuthorizationFilter
  11. {
  12. public virtual void OnAuthorization(HttpActionContext actionContext)
  13. {
  14. }
  15. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We want to intercept all exceptions")]
  16. Task<HttpResponseMessage> IAuthorizationFilter.ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
  17. {
  18. if (actionContext == null)
  19. {
  20. throw Error.ArgumentNull("actionContext");
  21. }
  22. if (continuation == null)
  23. {
  24. throw Error.ArgumentNull("continuation");
  25. }
  26. try
  27. {
  28. OnAuthorization(actionContext);
  29. }
  30. catch (Exception e)
  31. {
  32. return TaskHelpers.FromError<HttpResponseMessage>(e);
  33. }
  34. if (actionContext.Response != null)
  35. {
  36. return TaskHelpers.FromResult(actionContext.Response);
  37. }
  38. else
  39. {
  40. return continuation();
  41. }
  42. }
  43. }
  44. }