[英]ASP.Net Core 2.2 - Method overload appears in Visual Studio but does not work at run-time
I am attempting to verify that a user is authorized via a custom policy.我正在尝试验证用户是否通过自定义策略获得授权。 I followed the tutorial at Ode To Code to add this functionality to my controller.
我按照Ode To Code 中的教程将此功能添加到我的控制器中。 From within Visual Studio, the code appears to be correct and utilizing a known overload.
在 Visual Studio 中,代码似乎是正确的,并且使用了已知的重载。
Notice that it says that the overload is an "extension".请注意,它说重载是一个“扩展”。 I didn't take much notice of this until I spent 5 hours today trying to solve the following error:
直到今天我花了 5 个小时试图解决以下错误,我才注意到这一点:
As you can see, it would appear that the overload I'm attempting to use isn't being utilized.如您所见,我尝试使用的重载似乎没有被利用。 Am I doing something wrong here?
我在这里做错了吗? Is there something special I have to do to include these extended methods?
为了包含这些扩展方法,我需要做什么特别的事情吗? I've attempted cleaning and rebuilding the solution but this hasn't solved the problem.
我已经尝试清理和重建解决方案,但这并没有解决问题。
While you've defined the field for IAuthorizationSerivce
, you haven't provided any way for that to be set.虽然您已经为
IAuthorizationSerivce
定义了字段, IAuthorizationSerivce
您还没有提供任何设置方法。 You need to define a constructor for the LRController
that takes a single parameter of IAuthorizationService
, and assign that to the field.您需要定义的构造
LRController
,它利用一个参数IAuthorizationService
,并分配到外地。
I think there was a definition of that constructor in the tutorial.我认为教程中有该构造函数的定义。
Please note the name change: such as the global variable name for IAuthorizationService _authorization
has been prefixed with an underscore.请注意名称更改:例如 IAuthorizationService
_authorization
的全局变量名称已以下划线作为前缀。 Obviously not required, but as a good rule of thumb/good coding standard, IMO.显然不是必需的,但作为一个很好的经验法则/良好的编码标准,IMO。 :-)
:-)
public class LRController : Controller
{
private readonly IAuthorizationService _authorization;
// you're missing this constructor & this pattern is known as Constructor Dependency Injection
public LRController(IAuthorizationService authorization)
{
_authorization = authorization;
}
public async Task<RedirectToActionResult> Index()
{
var superAdmin = await _authorization.AuthorizeAsync(User, "IsLucky");
//rest of your code here
}
}
EDIT编辑
Additionally, if you wanted/needed to inject other interfaces into this controller, you would add it to that LRController constructor.此外,如果您想/需要将其他接口注入该控制器,您可以将其添加到该 LRController 构造函数中。 Would look something like this:
看起来像这样:
public class LRController : Controller
{
private readonly IAuthorizationService _authorization;
private readonly IOtherService _otherService;
public LRController(IAuthorizationService authorization, IOtherService otherService)
{
_authorization = authorization;
_otherService = otherService;
}
public async Task<RedirectToActionResult> Index()
{
var superAdmin = await _authorization.AuthorizeAsync(User, "IsLucky");
}
public async Task Foo()
{
await _otherService.Bar();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.