C# Can';找不到Request.GetOwinContext

C# Can';找不到Request.GetOwinContext,c#,asp.net-web-api,owin,C#,Asp.net Web Api,Owin,我已经搜索了一个小时,试图找出为什么这不起作用 我有一个带有WebAPI的ASP.NETMVC5应用程序。我正在尝试获取Request.GetOwinContext().Authentication,但似乎找不到如何包含GetOwinContext。这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using Sys

我已经搜索了一个小时,试图找出为什么这不起作用

我有一个带有WebAPI的ASP.NETMVC5应用程序。我正在尝试获取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.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Net.Http;
使用System.Web;
使用System.Web.Mvc;
使用System.Web.Security;
使用TaskPro.模型;
命名空间TaskPro.Controllers.api
{
公共类AccountController:ApicController
{
[HttpPost]
[异名]
public ReturnStatus登录(LoginView模型)
{ 
if(ModelState.IsValid)
{

var ctx=Request.GetOwinContext();//扩展方法位于
System.Web.Http.Owin
dll中,该dll需要作为nuget包下载(nuget包名为Microsoft.AspNet.WebApi.Owin)

请参见此处的msdn:

Nuget软件包在这里:

但是,该方法仍然是
System.Net.Http
命名空间的一部分,因此使用
定义应该可以

编辑

好的,为了澄清一些混淆:如果您使用的是ApiController(即
MyController:ApiController
),您将需要
Microsoft.AspNet.WebApi.Owin

如果您使用的是常规Mvc控制器(即
MyController:controller
),则需要
Microsoft.Owin.Host.SystemWeb
软件包


在MVC 5中,Api管道和常规MVC管道非常不同,但通常具有相同的命名约定。因此,一个管道中的扩展方法不适用于另一个管道。许多操作过滤器等也是如此。

在WEB Api中,您可以使用以下方法获得参考:

HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
using Microsoft.Owin;
using System.Web;

IOwinContext context = HttpContext.Current.GetOwinContext();
// or
IOwinContext context = HttpContext.Current.Request.GetOwinContext();
// Assembly Microsoft.Owin.Host.SystemWeb.dll, v3.1.0.0

using Microsoft.Owin;
using System;
using System.Runtime.CompilerServices;

namespace System.Web
{
    public static class HttpContextExtensions
    {
        public static IOwinContext GetOwinContext(this HttpContext context);
        public static IOwinContext GetOwinContext(this HttpRequest request);
    }
}
HttpContext.Current.GetOwinContext().GetUserManager();

它在Identity 2.0中工作

这些都不适用于我。我不得不将Nuget软件包与使用Identity创建的软件包进行比较,发现此Nuget软件包缺失,添加该软件包后,我解决了这个问题

Microsoft.Owin.Host.SystemWeb

显然,您需要它来使用ASP.NET请求管道在IIS上运行OWIN(请阅读,没有它您就完蛋了!)

您可能需要添加NuGet软件包
Microsoft.OWIN.Host.SystemWeb
,以便执行此操作:

HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
HttpContext.Current.GetOwinContext().GetUserManager();

我花了很长时间才找到一个简单的答案:但我所做的是使用启动时实例化的IOwinContext的单个实例的Get扩展。结果如下:

private readonly IOwinContext _iOwinContext = HttpContext.Current.GetOwinContext();

public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? _iOwinContext.Get<ApplicationUserManager>() ;
        }
        private set
        {
            _userManager = value;
        }
    }
private readonly IOwinContext\u IOwinContext=HttpContext.Current.GetOwinContext();
公共应用程序管理员用户管理器
{
得到
{
返回_userManager???_iOwinContext.Get();
}
专用设备
{
_userManager=value;
}
}
在GUI线程以外的线程中找不到
GetOwinContext()
扩展方法


因此,请注意不要从任何
wait
ed函数中调用此函数。

查看ASP.NET默认项目后,我发现我需要在应用程序启动时包含此函数:

// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in 
// with a third party login provider
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);`

我缺少用于ApicController的Microsoft.AspNet.WebApi.Owin包。
希望这有帮助!

我遇到了同样的问题,并通过使用私有方法解决了它:

专用IAAuthenticationManager AuthenticationManager
{
得到
{
返回HttpContext.Current.GetOwinContext().Authentication;
}
}


这对我来说非常有效。

我遇到了这个问题,我从nuget下载了额外的包来解决我的问题,(在package Manager控制台中运行以下命令)
在我的情况下,我需要添加

using Microsoft.AspNet.Identity.Owin;
用于解析GetOwinContext,然后在下一行中解析GetUserManager

Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
Request.GetOwinContext().GetUserManager();
(请注意,此答案适用于ASP.NET Web API,对应于问题中使用的标记。请查看您的查询是否与ASP.NET MVC有关。)

此问题未指定ASP.NET Web API服务的使用方式。中的对话框指示(重点):

基查拉于2014年10月24日凌晨1:35撰文

如果您不是自托管,请不要使用Microsoft.AspNet.WebApi.Owin
带有IIS的软件包…此软件包仅应与self一起使用 托管

建议在中使用Microsoft.AspNet.WebApi.Owin
软件包。在此回答中,我将报告在IIS中托管ASP.NET Web API服务时对我起到的作用

首先,安装以下NuGet软件包:

Microsoft.Owin.Host.SystemWeb

OWIN服务器,使基于OWIN的应用程序能够使用 ASP.NET请求管道

(请注意,在我写这篇文章的时候,这个NuGet软件包的最新可用版本是3.1.0。另外,在某种程度上,我使用的是Visual Studio 2013 Update 5。)

安装此NuGet软件包后,可以执行以下操作:

HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
using Microsoft.Owin;
using System.Web;

IOwinContext context = HttpContext.Current.GetOwinContext();
// or
IOwinContext context = HttpContext.Current.Request.GetOwinContext();
// Assembly Microsoft.Owin.Host.SystemWeb.dll, v3.1.0.0

using Microsoft.Owin;
using System;
using System.Runtime.CompilerServices;

namespace System.Web
{
    public static class HttpContextExtensions
    {
        public static IOwinContext GetOwinContext(this HttpContext context);
        public static IOwinContext GetOwinContext(this HttpRequest request);
    }
}
现在,让我们了解一下这些语句是如何解析的。在Visual Studio中,如果右键单击任一语句中的
GetOwinContext
,并选择“Peek Definition”,Visual Studio将显示以下内容:

HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
using Microsoft.Owin;
using System.Web;

IOwinContext context = HttpContext.Current.GetOwinContext();
// or
IOwinContext context = HttpContext.Current.Request.GetOwinContext();
// Assembly Microsoft.Owin.Host.SystemWeb.dll, v3.1.0.0

using Microsoft.Owin;
using System;
using System.Runtime.CompilerServices;

namespace System.Web
{
    public static class HttpContextExtensions
    {
        public static IOwinContext GetOwinContext(this HttpContext context);
        public static IOwinContext GetOwinContext(this HttpRequest request);
    }
}
如您所见,在
System.Web
命名空间中,有两种
GetOwinContext
扩展方法:

  • 一个在
    HttpContext
    类上,以及
  • 另一个在
    HttpRequest
    类上

同样,对于那些在IIS中托管Web API服务的人,我希望这能消除任何关于2017年晚些时候在哪里可以找到
GetOwinContext
定义的歧义。

我不得不添加package
Microso