找不到 Request.GetOwinContext [英] Can't find Request.GetOwinContext
问题描述
我一直在寻找一个小时,试图弄清楚为什么这不起作用.
I have been searching for an hour trying to figure out why this isn't working.
我有一个带有 WebAPI 的 ASP.Net MVC 5 应用程序.我正在尝试获取 Request.GetOwinContext().Authentication,但是我似乎无法找到如何包含 GetOwinContext.这是我的代码:
I have a ASP.Net MVC 5 application with a WebAPI. I am trying to get Request.GetOwinContext().Authentication, however I can't seem to find how to include GetOwinContext. Here is my code:
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 也不会给我任何智能感知选项.
From what I've read, it should be part of the System.Net.Http, but I've included that and it still isn't resolving. Ctrl-Space doesn't give me any intellisense options either.
我在这里遗漏了什么?
推荐答案
GetOwinContext
扩展方法位于 System.Web.Http.Owin
dll 中,需要下载为nuget包(nuget包名称为Microsoft.AspNet.WebApi.Owin)
The GetOwinContext
extension method is in the System.Web.Http.Owin
dll which needs to be downloaded as a nuget package (The nuget package name is Microsoft.AspNet.WebApi.Owin)
Install-Package Microsoft.AspNet.WebApi.Owin
See msdn here: http://msdn.microsoft.com/en-us/library/system.net.http.owinhttprequestmessageextensions.getowincontext(v=vs.118).aspx
此处的 Nuget 包:https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Owin
Nuget package here: https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Owin
但是,该方法仍然是 System.Net.Http
命名空间的一部分,因此您拥有的 using
定义应该没问题.
However, the method is still part of the System.Net.Http
namespace, so the using
definitions you have should be fine.
编辑
好的,澄清一些混淆:如果您使用的是 ApiController(即 MyController : ApiController
),您将需要 Microsoft.AspNet.WebApi.Owin
包.
Okay, to clear up some confusion: If you are using an ApiController (i.e MyController : ApiController
) you will require the Microsoft.AspNet.WebApi.Owin
package.
如果您使用的是常规 Mvc 控制器(即 MyController : Controller
),您将需要 Microsoft.Owin.Host.SystemWeb
包.
If you are using a regular Mvc controller (i.e. MyController : Controller
) you will need the Microsoft.Owin.Host.SystemWeb
package.
在 MVC 5 中,Api 和常规 MVC 的管道非常不同,但通常具有相同的命名约定.所以一个扩展方法不适用于另一个.许多动作过滤器等也是如此.
In MVC 5 the pipelines for Api and regular MVC were very different, but often have the same naming conventions. So an extension method in one does not apply to the other. Same for a lot of the action filters etc.
这篇关于找不到 Request.GetOwinContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!