找不到请求

我已经找了一个小时想弄明白为什么这样不行。

我有一个带有 WebAPI 的 ASP.Net MVC 5应用程序。我正在接听“请求”。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);
}
}
}

据我所知,它应该是系统的一部分。网。但是我已经把它包括进去了,还是没有解决。Ctrl-Space 也没有给我任何智能感知选项。

我错过了什么?

93376 次浏览

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 package here: https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Owin

However, the method is still part of the System.Net.Http namespace, so the using definitions you have should be fine.

EDIT

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.

If you are using a regular Mvc controller (i.e. MyController : Controller) you will need the Microsoft.Owin.Host.SystemWeb package.

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.

In WEB API, you can get the reference using the following:

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

it works in Identity 2.0

None of these worked for me. I had to compare Nuget packages with one that was created with Identity and I found this Nuget package missing which when added fixed the issue for me

Microsoft.Owin.Host.SystemWeb

Apparently you need it to run OWIN on IIS using the ASP.NET request pipeline (read you're screwed without it!)

You may need to add the NuGet package Microsoft.Owin.Host.SystemWeb in order to do this:

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

This took me forever to find a simple answer: but what I did was use the Get extension of the single instance of the IOwinContext that was instantiated in the startup. So it came out like this:

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


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

The GetOwinContext() extension method is not found in threads other than the GUI thread.

So be careful to not call this function from within any awaited function.

After looking at the ASP.NET default project I discovered I needed to include this in my application startup:

// 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);`

I was missing package: Microsoft.AspNet.WebApi.Owin for ApiControllers. Hope this helps!

I had this same problem and solved it by using a private method: private IAuthenticationManager AuthenticationManager { get { return HttpContext.Current.GetOwinContext().Authentication; } }

This worked swimmingly for me.

I have this problem and I download extra package from nuget to solve my problem,(run following command in Package Manager Console) Install-Package Microsoft.Owin.Host.SystemWeb

In my case I need to add

using Microsoft.AspNet.Identity.Owin;

for resolving GetOwinContext and then GetUserManager in following line.

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

(Please note that this answer is for ASP.NET Web API which corresponds to the tag used on the question. See this question if your inquiry is with respect to ASP.NET MVC.)

The question does not specify how the ASP.NET Web API service is to be hosted. The dialog in this post indicates (emphasis mine):

kichalla wrote Oct 24, 2014 at 1:35 AM

If you are NOT self-hosting, do not use Microsoft.AspNet.WebApi.Owin package with IIS...this package is only supposed to be used with self hosting.

Use of the Microsoft.AspNet.WebApi.Owin package is recommended in the accepted answer. In this answer I am reporting what has worked for me when hosting an ASP.NET Web API service in IIS.

First, install the following NuGet package:

Microsoft.Owin.Host.SystemWeb

OWIN server that enables OWIN-based applications to run on IIS using the ASP.NET request pipeline.

(Note that as I write, the latest available version of this NuGet package is 3.1.0. Also, to the extent that it might matter, I am using Visual Studio 2013 Update 5.)

After installing this NuGet package, you can do the following:

using Microsoft.Owin;
using System.Web;


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

Now, to shed some light on how these statements are resolved. In Visual Studio, if you right-click GetOwinContext in either statement and select "Peek Definition," Visual Studio will display the following:

// 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);
}
}

As you can see, within the System.Web namespace, there are two GetOwinContext extension methods:

  • One on the HttpContext class, and
  • The other on the HttpRequest class.

Again, for those hosting their Web API service in IIS, my hope is that this clears up any ambiguity regarding where a definition for GetOwinContext can be found with respect to this late date in 2017.

I had to add package Microsoft.AspNet.Identity.Owin