Exemple #1
0
        public ODataAuthorizationTest()
        {
            var model = TestModel.GetModelWithPermissions();

            var controllers = new[]
            {
                typeof(ProductsController),
                typeof(MyProductController),
                typeof(RoutingCustomersController),
                typeof(VipCustomerController),
                typeof(SalesPeopleController),
                typeof(TodoItemController),
                typeof(IncidentsController),
                typeof(IncidentGroupsController)
            };

            var server = TestServerFactory.CreateWithEndpointRouting(controllers, endpoints =>
            {
                endpoints.MapODataRoute("odata", "odata", model);
            }, services =>
            {
                services.AddODataAuthorization((options) =>
                {
                    options.ScopesFinder = (context) =>
                    {
                        var permissions = context.User?.FindAll("Permission").Select(p => p.Value);
                        return(Task.FromResult(permissions ?? Enumerable.Empty <string>()));
                    };

                    options.ConfigureAuthentication("AuthScheme")
                    .AddScheme <CustomAuthOptions, CustomAuthHandler>("AuthScheme", options => { });
                });

                services.AddRouting();
            }, app =>
            {
                app.UseAuthentication();
                app.UseODataAuthorization();
            });

            _client = TestServerFactory.CreateClient(server);
        }