Giter Site home page Giter Site logo

Comments (3)

sturlath avatar sturlath commented on July 4, 2024

So no recommendation on how to do this?

from rimdev.featureflags.

kendaleiv avatar kendaleiv commented on July 4, 2024

That is an excellent question. @billbogaiv implemented something like this:

.UseWhen(
    predicate: x =>
    {
        return
        x.Request.Path.StartsWithSegments(featureFlagsOptions.ApiGetAllPath) ||
        x.Request.Path.StartsWithSegments(featureFlagsOptions.ApiGetPath) ||
        x.Request.Path.StartsWithSegments(featureFlagsOptions.ApiSetPath) ||
        x.Request.Path.StartsWithSegments(featureFlagsOptions.UiPath);
    },
    configuration: x =>
    {
        // Custom authorization code, for example:
        x.UseAuthorization(OpenIdConnectDefaults.AuthenticationScheme);
    })

I'd like to see something cleaner in the future, but maybe this helps for the moment?

from rimdev.featureflags.

billbogaiv avatar billbogaiv commented on July 4, 2024

Some additional things you'll need (assuming < .NET Core 3):

/// <summary>
/// Consider removing once .NET Core 3.0 is stable => relies on EndpointRouting to work.
/// Ref. https://github.com/aspnet/AspNetCore/blob/v3.0.0-preview3-19153-02/src/Security/Authorization/Policy/src/AuthorizationAppBuilderExtensions.cs
/// </summary>
public static class AuthorizationAppBuilderExtensions
{
    public static IApplicationBuilder UseAuthorization(
        this IApplicationBuilder app,
        string policyName)
    {
        if (string.IsNullOrEmpty(policyName))
        {
            throw new ArgumentNullException(nameof(policyName));
        }

        return app.UseMiddleware<AuthorizationMiddleware>(policyName);
    }
}

and

/// <summary>
/// Consider removing once .NET Core 3.0 is stable => relies on EndpointRouting to work.
/// Ref. https://github.com/aspnet/AspNetCore/blob/v3.0.0-preview3-19153-02/src/Security/Authorization/Policy/src/AuthorizationMiddleware.cs.
/// </summary>
public class AuthorizationMiddleware
{
    public AuthorizationMiddleware(
        RequestDelegate next,
        string policyName)
    {
        this.next = next;
        this.policyName = policyName;
    }

    private readonly RequestDelegate next;
    private readonly string policyName;

    public async Task Invoke(
        HttpContext httpContext,
        IAuthorizationService authorizationService)
    {
        AuthorizationResult authorizationResult =
            await authorizationService.AuthorizeAsync(httpContext.User, null, policyName);

        if (!httpContext.User.Identity.IsAuthenticated && !authorizationResult.Succeeded)
        {
            await httpContext.ChallengeAsync();

            return;
        }
        else if (httpContext.User.Identity.IsAuthenticated && !authorizationResult.Succeeded)
        {
            await httpContext.ForbidAsync();

            return;
        }

        await next(httpContext);
    }
}

from rimdev.featureflags.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.