English 中文(简体)
如何将参数传给习俗归属协会。 NET 核心网络预报?
原标题:How to pass a parameter to a custom attribute ASP.NET Core Web API?

我想产生一种习俗特性,即如果用户在进入APIC途径之前能够获得资源,则将予以检查。 理想的情况是,在这条路上会看像这样的东西。

[HttpGet]
[Route("GetOrders")]
[SecurityFunctionFilter("GETORDERS")]   // <-- desired attribute
public IActionResult GetOrders()
{
    ActionResult response;
    try
    {
        response = Ok();
    }
    catch
    {
        response = new StatusCodeResult(500);
    }
    return response;
}

为了落实这一点,我试图创建一种过滤器,延伸到阿特里亚特,并落实下文所述的伊特里亚化基金接口。 控制器路上的沥青将穿透安全功能,该功能将设于安全功能基金建筑商。 这将受到检查,如果收到虚假信息,将返回APIC404。

public class SecurityFunctionFilter : Attribute, IAuthorizationFilter
{
    private readonly string _securityFunction;

    public SecurityFunctionFilter(string securityFunction)
    {
        _securityFunction=securityFunction;
    }

    public void OnAuthorization(AuthorizationFilterContext context)
    {
            // Not yet implemented, will check the users username against security function and return bool
        if (_securityFunction == "GETORDERS")
        {
            context.Result = new StatusCodeResult(403);
        }
    }
}

Inside my program.cs, I did the following:

public static void Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);

    // Add services to the container.

    builder.Services.AddControllers();

    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();

    builder.Services.AddCors(p => p.AddPolicy("corsapp", builder =>
    {
        builder.WithOrigins("*").AllowAnyMethod().AllowAnyHeader();
    }));

    builder.Services.AddScoped<SecurityFunctionFilter>();


    var app = builder.Build();

    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI();
        app.UseCors("corsapp");
    }

    app.UseHttpsRedirection();

    app.UseAuthorization();


    app.MapControllers();

    app.Run();
}

在试图操作时,有人向建筑商投掷错误。 建设。 这是错误:

Some services are not able to be constructed (Error while validating the service descriptor  ServiceType: testing_proj_api.Filters.SecurityFunctionFilter Lifetime: Scoped ImplementationType: testing_proj_api.Filters.SecurityFunctionFilter : Unable to resolve service for type  System.String  while attempting to activate  testing_proj_api.Filters.SecurityFunctionFilter .)

SOLUTION:

我需要做的是,从方案.cs的服务登记册中删除安全功能基金。

问题回答

Some services are not able to be constructed (Error while validating the service descriptor ServiceType: testing_proj_api.Filters.SecurityFunctionFilter Lifetime: Scoped ImplementationType: testing_proj_api.Filters.SecurityFunctionFilter : Unable to resolve service for type System.String while attempting to activate testing_proj_api.Filters.SecurityFunctionFilter .)

诚然,根据我的考验,根据你的ros子和描述,你似乎不需要把安全功能规范登记为国际功能。 由于这只需要援引,而控制者将受到打击,因此我们无需登记为DI。 此外,你登记增补的DI的方式也是错误的。

Here I have reproduced your issue and get rid of the following service and working as expected:

不需要这一服务:

builder.Services.AddScoped<SecurityFunctionFilter>();

另一重要一点是,这一错误不是因为你的行动过滤器而被推倒的,而是因为使用“补充”服务是不正确的。 登记补充服务的适当途径是:

builder.Services.AddScoped<IMyDependency, MyDependency>();

Note: AddScoped service also requires its Interface in order to register.

Output:

enter image description here

<>说明: 请





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签