无法找到Request.GetOwinContext。
无法找到Request.GetOwinContext。
我已经搜索了一个小时,试图弄清楚为什么这没用。
我有一个ASP.Net MVC 5应用程序和一个WebAPI。我正在尝试获取Request.GetOwinContext().Authentication, 但我似乎找不到如何包含GetOwinContext。 这是我的代码:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web; using System.Web.Mvc; using System.Web.Security; using TaskPro.Models; namespace TaskPro.Controllers.api { public class AccountController : ApiController { [HttpPost] [AllowAnonymous] public ReturnStatus Login(LoginViewModel model) { if (ModelState.IsValid) { var ctx = Request.GetOwinContext(); // <-- Can't find this return ReturnStatus.ReturnStatusSuccess(); } return base.ReturnStatusErrorsFromModelState(ModelState); } } }
根据我所看到的,它应该是System.Net.Http的一部分,但我已经包含了它,它仍然没有解析。 Ctrl-Space也没有给我任何智能感知选项。
我在这里错过了什么?
admin 更改状态以发布 2023年5月24日
GetOwinContext
扩展方法在System.Web.Http.Owin
dll中,需要通过下载nuget包来获取(nuget包的名称是Microsoft.AspNet.WebApi.Owin)。
Install-Package Microsoft.AspNet.WebApi.Owin
Nuget包在这里: https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Owin
然而,该方法仍然是System.Net.Http
命名空间的一部分,因此您的using
定义应该没问题。
编辑
好吧,为了消除一些混淆:如果您正在使用ApiController(即MyController : ApiController
),则需要Microsoft.AspNet.WebApi.Owin
包。
如果您正在使用常规的Mvc控制器(即MyController : Controller
),则需要Microsoft.Owin.Host.SystemWeb
包。
在MVC 5中,Api和常规MVC的管道非常不同,但通常具有相同的命名约定。因此,一个命名不同的扩展方法不适用于另一个。同样,许多操作过滤器等也是如此。