gpt4 book ai didi

c# - 如何访问 ASP.NET Core 服务中的路由数据/值提供程序数据?

转载 作者:太空狗 更新时间:2023-10-29 22:29:44 25 4
gpt4 key购买 nike

我正在尝试写一个 Policy-based Authorization Handler . handler的业务逻辑需要使用默认路由传入的当前请求的记录id

[Authorize(Roles = "TaskAdmin", Policy = "RecordOwner")]
public IActionResult Index(int id) // <-- Need this id
{
// <snip>

return View();
}

政策

这是我需要访问 id 路由值的类。

public class RecordOwnerHandler : AuthorizationHandler<RecordOwnerRequirement>
{
private readonly ApplicationDbContext dbContext;

public RecordOwnerHandler(ApplicationDbContext dbContext)
{
this.dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}

protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, RecordOwnerRequirement requirement)
{
if (IsUserAuthorized(context))
{
context.Succeed(requirement);
}

//TODO: Use the following if targeting a version of
//.NET Framework older than 4.6:
// return Task.FromResult(0);
return Task.CompletedTask;
}

private bool IsUserAuthorized(AuthorizationHandlerContext context)
{
//****************************************
// Need the id here...
//****************************************

// Return the result
return true;
}
}

启动

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

// *** Add policy for record owner ***
services.AddAuthorization(options =>
{
options.AddPolicy("RecordOwner", policy =>
policy.Requirements.Add(new RecordOwnerRequirement()));
});

// Add application services.
services.AddTransient<IEmailSender, EmailSender>();

// *** Register record owner handler with the DI container ***
services.AddTransient<IAuthorizationHandler, RecordOwnerHandler>();

services.AddMvc();
}

我尝试了什么

  1. 我尝试使用 IHttpContextAccessor 作为 RecordOwnerHandler 的构造函数参数,但是 IHttpContextAccessor.HttpContext 似乎不包含请求的 RouteData

  2. 我进行了几次 Google 搜索,看看是否有任何关于如何执行此操作的信息,但结果一无所获。

  3. 然后我研究了RoutingModel Binding 的源代码,但似乎找不到用于注入(inject)路由值的抽象进入服务。

I realize I could try to parse this info out of the URL, but I am hoping for a cleaner way to get the value.

那么,如何在 ASP.NET Core 2.0 的服务中访问路由值和/或值提供者数据?

最佳答案

可以使用 ActionContextAccessor class 访问路由值.

直接投资注册

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

用法

public class RecordOwnerHandler : AuthorizationHandler<RecordOwnerRequirement>
{
private readonly ApplicationDbContext dbContext;
private readonly IActionContextAccessor actionContextAccessor;

public RecordOwnerHandler(ApplicationDbContext dbContext, IActionContextAccessor actionContextAccessor)
{
this.dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
this.actionContextAccessor = actionContextAccessor ?? throw new ArgumentNullException(nameof(actionContextAccessor));
}

protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, RecordOwnerRequirement requirement)
{
if (IsUserAuthorized(context))
{
context.Succeed(requirement);
}

//TODO: Use the following if targeting a version of
//.NET Framework older than 4.6:
// return Task.FromResult(0);
return Task.CompletedTask;
}

private bool IsUserAuthorized(AuthorizationHandlerContext context)
{
// Now the id route value can be accessed directly...
var id = this.actionContextAccessor.ActionContext.RouteData.Values["id"];

// Use the dbContext to compare the id against the database...

// Return the result
return true;
}
}

NOTE: I would still like to find out a way to access the value providers to do this, so it wouldn't matter if the parameter is passed through route values, query string, form values, etc.

关于c# - 如何访问 ASP.NET Core 服务中的路由数据/值提供程序数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48812573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com