基于路由值的全局资源授权 - c#

我正在使用ASP.Net Core 3.1 Web API。

API用户来自Azure AD。
如果用户具有许可证,则可以访问API,可以将每个用户分配给多个许可证,并且可以将同一许可证分配给多个用户。

客户希望我使用<api_url>/{licenseId}/controller/action之类的模板来构造API路由。

我的控制器就像:

[Authorize]
[Route("{licenseId}/Foo")]
public class FooController : ControllerBase
{

如果我将许可证视为资源,则可以使用基于资源的授权,如here所述。
它可以工作,但是我发现自己复制并粘贴了所有操作的身份验证检查。

有没有更好的方法来使用路由值授权用户?

这是到目前为止我得到的:

public class LicenseRequirement : IAuthorizationRequirement
{        
    public Guid LicenseId { get; private set; }

    public LicenseRequirement(Guid licenseId)
    {
        LicenseId = licenseId;
    }
}

public class LicenseAuthorizationHandler : AuthorizationHandler<LicenseRequirement>
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly ILogger<LicenseAuthorizationHandler> _logger;
    private readonly DBContext _db;

    public LicenseAuthorizationHandler(DBContext context, ILogger<LicenseAuthorizationHandler> logger, IHttpContextAccessor httpContextAccessor)
    {
        _logger = logger;
        _db = context;
        _httpContextAccessor = httpContextAccessor;
    }

    protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, LicenseRequirement requirement)
    {
        var userId = new Guid(context.User.GetUserId());
        var licenseId = _httpContextAccessor.HttpContext.GetRouteData().Values["licenseId"];

        if (await _db.ApiUsers.SingleOrDefaultAsync(x => x.LicenseId == new Guid(licenseId as string) && x.UserId == userId) is ApiUser user)
            context.Succeed(requirement);
    }
}

现在,我有点不知所措,因为我不知道如何在Startup.cs中进行设置并将其用作属性或区域过滤器,并在运行时使用licenseId路由的值创建这些要求。

参考方案

我发现IAuthorizationFilter非常容易实现。昨天尝试时,找不到RouteValues,但是它们在那儿:

public class LicenseAuthorizationFilter : IAuthorizationFilter
{
    private readonly ILogger<LicenseAuthorizationFilter> _logger;
    private readonly DBContext _db;

    public LicenseAuthorizationFilter(DBContext context, ILogger<LicenseAuthorizationFilter> logger)
    {
        _logger = logger;
        _db = context;
    }

    public void OnAuthorization(AuthorizationFilterContext context)
    {
        var userId = new Guid(context.HttpContext.User.GetUserId());
        var licenseId = new Guid(context.HttpContext.Request.RouteValues["licenseId"] as string);

        if (!(_db.ApiUsers.SingleOrDefault(x => x.LicenseId == licenseId && x.UserId == userId) is ApiUser user))
        {
            context.Result = new ForbidResult();
        }
    }
}

public class LicenseAuthorizationAttribute : TypeFilterAttribute
{
    public LicenseAuthorizationAttribute() : base(typeof(LicenseAuthorizationFilter))
    { }
}

控制器可以整齐地变成:

[Authorize]
[LicenseAuthorization]
[Route("{licenseId}/Items")]
public class ItemsController : ControllerBase
{
    [...]
}

ASP.NET Core Singleton实例与瞬态实例的性能 - c#

在ASP.NET Core依赖注入中,我只是想知道注册Singleton实例是否会比注册Transient实例更好地提高性能?在我看来,对于Singleton实例,创建新对象和相关对象只需花费一次时间。对于Transient实例,此成本将针对每个服务请求重复。因此Singleton似乎更好。但是,在Singleton上使用Transient时,我们可以获得多…

ASP.NET Core-在Singleton注入上存储库依赖项注入失败 - c#

我正在使用SoapCore为我的ASP.NET Core MVC应用程序创建Web服务。我正在使用Entity Framework Core和简单的存储库模式来获取我的数据库数据。我通过Startup.cs中的.AddSingleton()注入存储库类:services.AddSingleton<IImportRepository, ImportRep…

如何在ASP.NET Core Web应用程序中增加JSON反序列化MaxDepth限制 - c#

我们正在将ASP.NET Core 2.1与.NET Framework 4.6.2结合使用。我们有一个客户需要向我们的Web应用程序发送一个很大程度上嵌套的json结构。当他们进行此调用时,我们将输出以下日志并返回错误: 读取器的MaxDepth超过了32。路径“ super.long.path.to property”,第1行,位置42111。”我浏览了…

ASP.NET MVC Core 3.0 API将枚举序列化为字符串 - c#

如何在ASP.NET MVC Core 3.0中将Enum字段序列化为String而不是Int?我不能用旧的方式做。services.AddMvc().AddJsonOptions(opts => { opts.JsonSerializerOptions.Converters.Add(new StringEnumConverter()); }) 我收到…

Asp.net Core 2.1记录 - c#

我想了解登录asp.net cortex 2.1的问题。我的目标是注册所有可能导致kestrel服务器损坏的严重错误。据我从文档中了解Microsoft.Extensions.Logging和Serilog,您需要在每个控制器中创建一个记录器,并设置记录条件(例如try / catch)。是否有一种更方便的方法可以从一个地方记录项目中的任何严重错误?我将不胜…