【发布时间】:2019-02-22 23:28:36
【问题描述】:
我正在创建一个 webapi,我想创建一个从 ControllerBase 类继承的控制器。
我这样做是因为在调用操作之前我需要做一些事情,即检查用户注册并使用 HttpContext。
public class BaseApiZrController : ControllerBase
{
public BaseApiZrController(ApplicationDbContext db)
{
this.HandleAuthentication();
//??? this.HttpContext is always null, how come?
ClaimsPrincipal claimsPrincipal = this.HttpContext.User;
}
//...some code
}
这是一个好习惯还是我应该通过中间件在开始类中做所有这些?
另外一个问题,我自定义的BaseApiZrController类继承ControllerBase类时,无法访问httpContext,总是返回null,怎么回事?
【问题讨论】:
-
对于身份验证和授权,请务必查看 ASP.NET Core Security and Identity 文档中的这些部分。这可以通过配置(在 Startup.cs 中)、操作属性和对授权服务的调用(用于基于资源的授权)轻松处理,而无需使用中间件或类似的东西。
标签: .net-core asp.net-core-webapi httpcontext