找不到Request.GetOwinContext

问题描述 投票:88回答:13

我一直在寻找一个小时试图弄清楚为什么这不起作用。

我有一个带有WebAPI的ASP.Net MVC 5应用程序。我正在尝试获取Request.GetOwinContext()。身份验证,但我似乎无法找到如何包含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也没有给我任何intellisense选项。

我在这里错过了什么?

c# asp.net-web-api owin
13个回答
146
投票

GetOwinContext扩展方法在System.Web.Http.Owin dll中,需要下载为nuget包(nuget包名称为Microsoft.AspNet.WebApi.Owin)

Install-Package Microsoft.AspNet.WebApi.Owin

请参见msdn: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

但是,该方法仍然是System.Net.Http命名空间的一部分,因此你所拥有的using定义应该没问题。

编辑

好吧,要澄清一些困惑:如果你使用ApiController(即MyController : ApiController),你将需要Microsoft.AspNet.WebApi.Owin包。

如果您使用常规的Mvc控制器(即MyController : Controller),您将需要Microsoft.Owin.Host.SystemWeb包。

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


1
投票

我错过了包:ApiControllers的Microsoft.AspNet.WebApi.Owin。希望这可以帮助!


1
投票

我有同样的问题,并通过使用私有方法解决它: private IAuthenticationManager AuthenticationManager { get { return HttpContext.Current.GetOwinContext().Authentication; } }

这对我来说很有效。


1
投票

(请注意,这个答案适用于ASP.NET Web API,它对应于问题中使用的标记。如果您的查询是关于ASP.NET MVC,请参阅this question。)

问题没有说明ASP.NET Web API服务是如何成为hosted的。 this post中的对话框表明(强调我的):

kichalla于2014年10月24日上午1:35写道

如果您不是自托管,请不要将Microsoft.AspNet.WebApi.Owin包与IIS一起使用...此包仅应与自托管一起使用。

Microsoft.AspNet.WebApi.Owin建议使用accepted answer包装。在这个答案中,我报告了在IIS中托管ASP.NET Web API服务时对我有用的内容。

首先,安装以下NuGet包:

Microsoft.Owin.Host.SystemWeb

OWIN服务器,它使基于OWIN的应用程序能够使用ASP.NET请求管道在IIS上运行。

(请注意,在我写的时候,这个NuGet包的最新可用版本是3.1.0。另外,在可能重要的范围内,我使用的是Visual Studio 2013 Update 5.)

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

using Microsoft.Owin;
using System.Web;

IOwinContext context = HttpContext.Current.GetOwinContext();
// or
IOwinContext context = HttpContext.Current.Request.GetOwinContext();

现在,阐明如何解决这些陈述。在Visual Studio中,如果在任一语句中右键单击GetOwinContext并选择“Peek Definition”,Visual Studio将显示以下内容:

// 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服务的人,我希望这清除了关于GetOwinContext的定义在2017年这个晚期可以找到的任何歧义。


0
投票

我不得不添加包Microsoft.AspNet.Identity.Owin


47
投票

这些都不适合我。我不得不将Nuget软件包与使用Identity创建的软件包进行比较,我发现这个Nuget软件包丢失,添加时修复了我的问题

Microsoft.Owin.Host.SystemWeb

显然你需要它使用ASP.NET请求管道在IIS上运行OWIN(如果没有它,请阅读你!)


42
投票

在WEB API中,您可以使用以下内容获取引用:

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

它适用于Identity 2.0


20
投票

您可能需要添加NuGet包Microsoft.Owin.Host.SystemWeb才能执行此操作:

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

7
投票

在我的情况下,我需要添加

using Microsoft.AspNet.Identity.Owin;

用于解析GetOwinContext,然后在以下行中解析GetUserManager。

Request.GetOwinContext().GetUserManager<ApplicationUserManager>();

3
投票

我有这个问题,我从nuget下载额外的软件包来解决我的问题,(在软件包管理器控制台中运行以下命令)Install-Package Microsoft.Owin.Host.SystemWeb


2
投票

这让我永远找到了一个简单的答案:但我所做的是使用在启动时实例化的IOwinContext的单个实例的Get扩展。所以它出来是这样的:

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

public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? _iOwinContext.Get<ApplicationUserManager>() ;
        }
        private set
        {
            _userManager = value;
        }
    }

1
投票

在GUI线程以外的线程中找不到GetOwinContext()扩展方法。

因此,请注意不要在任何awaited函数中调用此函数。


1
投票

在查看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);`
© www.soinside.com 2019 - 2024. All rights reserved.