/src/System.Web.Http/Filters/AuthorizationFilterAttribute.cs
C# | 49 lines | 43 code | 5 blank | 1 comment | 6 complexity | c4b7ffca6c28c7c73cee89300b23fe72 MD5 | raw file
- // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
- using System.Diagnostics.CodeAnalysis;
- using System.Net.Http;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Web.Http.Controllers;
- namespace System.Web.Http.Filters
- {
- [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
- public abstract class AuthorizationFilterAttribute : FilterAttribute, IAuthorizationFilter
- {
- public virtual void OnAuthorization(HttpActionContext actionContext)
- {
- }
- [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We want to intercept all exceptions")]
- Task<HttpResponseMessage> IAuthorizationFilter.ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
- {
- if (actionContext == null)
- {
- throw Error.ArgumentNull("actionContext");
- }
- if (continuation == null)
- {
- throw Error.ArgumentNull("continuation");
- }
- try
- {
- OnAuthorization(actionContext);
- }
- catch (Exception e)
- {
- return TaskHelpers.FromError<HttpResponseMessage>(e);
- }
- if (actionContext.Response != null)
- {
- return TaskHelpers.FromResult(actionContext.Response);
- }
- else
- {
- return continuation();
- }
- }
- }
- }