public async Task HandleAsync(AuthorizationHandlerContext context)
        {
            if (_authContextHook != null)
            {
                try
                {
                    if (_authContextHook?.OnReceived != null)
                    {
                        _authContextHook.OnReceived(context);
                    }
                    await _handler.HandleAsync(context);

                    if (_authContextHook?.OnProcessed != null)
                    {
                        _authContextHook.OnProcessed(context);
                    }
                }
                catch (Exception ex)
                {
                    if (_authContextHook?.OnErrored != null)
                    {
                        _authContextHook.OnErrored(ex, context);
                    }
                    throw;
                }
            }
            else
            {
                await _handler.HandleAsync(context);
            }
        }
Exemplo n.º 2
0
        public async Task <bool> IsAuthorized()
        {
            try
            {
                var context = new AuthorizationHandlerContext(requirements, user, resource);
                await authorizationHandler.HandleAsync(context);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        public static async void AssertHandlerHandlesScenarioSuccessfully <TEntity, TRequirement>(
            IAuthorizationHandler handler,
            HandlerTestScenario <TEntity> scenario)
            where TRequirement : IAuthorizationRequirement
        {
            var authContext = new AuthorizationHandlerContext(
                new IAuthorizationRequirement[] { Activator.CreateInstance <TRequirement>() },
                scenario.User, scenario.Entity);

            await handler.HandleAsync(authContext);

            if (scenario.ExpectedToPass)
            {
                Assert.True(authContext.HasSucceeded, scenario.UnexpectedFailMessage);
            }
            else
            {
                Assert.False(authContext.HasSucceeded, scenario.UnexpectedPassMessage);
            }
        }