Skip to content

Question: Construct EdmModels on a per request basis? #1134

Open
@aniliht

Description

@aniliht

Hello,
Thank you for the support and maintaining this library. I'm in the process of updating my code from
.NET6 and OData v7

    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.OData" Version="7.7.1" />
    <PackageReference Include="Microsoft.AspNetCore.OData.Versioning" Version="4.1.1" />

to .NET8 and OData v8 and updated versioning:

    <PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
    <PackageReference Include="Asp.Versioning.OData" Version="8.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.OData" Version="8.2.7" />

In my old code, I was able to construct the OData EDM Models on a per request basis (depending on the user's claims). So the structure of the EDMs would vary based on the user's claims. I do this to hide certain entitySets that certain users shouldn't have access to OR not expose certain properties on a specific entitys. I'm wondering if there's a way to do it similarly in the newer library?

How I did it in my old code:

            app.UseMvc(routeBuilder =>
            {
                routeBuilder.Count();
                routeBuilder.SetTimeZoneInfo(TimeZoneInfo.Utc);
                routeBuilder.MapVersionedODataRoute(routeNameV1, string.Empty, new ApiVersion(1, 0), GetODataContainerBuilder(routeNameV1, routeBuilder));
            });


        private Action<IContainerBuilder> GetODataContainerBuilder(string routeName, IRouteBuilder routeBuilder)
        {
            return containerBuilder =>
            {
                containerBuilder.AddService(Microsoft.OData.ServiceLifetime.Scoped, sp =>
                {
                    var httpRequestScope = sp.GetRequiredService<HttpRequestScope>();
                    var claims = httpRequestScope?.HttpRequest?.HttpContext?.User?.Claims;
                    if (httpRequestScope?.HttpRequest?.HttpContext?.User?.Identity?.IsAuthenticated == true)
                    {
                        return EdmModelFactory.GetEdmModel(claims);
                    }
                    else
                    {
                        return EdmModelFactory.Empty;
                    }
                });

                containerBuilder.AddService<IEnumerable<IODataRoutingConvention>>(Microsoft.OData.ServiceLifetime.Singleton, sp =>
                    ODataRoutingConventions.CreateDefaultWithAttributeRouting(routeName, routeBuilder));
            };

        }

For now, I'm using pretty much the pattern shown in the provided examples and am planning to workaround not being able to dynamically generate EDM's based on the User's claims.

Thank you!

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions