Request.GetOwinContext를 찾을 수 없습니다.
왜 이것이 작동하지 않는지 알아 내려고 한 시간 동안 검색했습니다.
WebAPI가있는 ASP.Net MVC 5 응용 프로그램이 있습니다. 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는 인텔리 센스 옵션도 제공하지 않습니다.
내가 여기서 무엇을 놓치고 있습니까?
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의 파이프 라인은 매우 달랐지만 종종 동일한 명명 규칙을 사용합니다. 따라서 하나의 확장 방법은 다른 하나에 적용되지 않습니다. 많은 액션 필터 등에 대해 동일합니다.
이것들 중 어느 것도 나를 위해 일하지 않았습니다. Nuget 패키지를 Identity로 만든 패키지와 비교해야했는데이 Nuget 패키지가 누락되어 추가되었을 때 문제가 해결되었습니다.
Microsoft.Owin.Host.SystemWeb
ASP.NET 요청 파이프 라인을 사용하여 IIS에서 OWIN을 실행하려면이 기능이 필요합니다.
WEB API에서 다음을 사용하여 참조를 얻을 수 있습니다.
HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
그것은 Identity 2.0에서 작동합니다
이렇게하려면 NuGet 패키지를 추가해야 할 수 있습니다 Microsoft.Owin.Host.SystemWeb
.
HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
제 경우에는 추가해야합니다
using Microsoft.AspNet.Identity.Owin;
다음 줄에서 GetOwinContext 및 GetUserManager를 확인합니다.
Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
이 문제가 있으며 문제를 해결하기 위해 nuget에서 추가 패키지를 다운로드합니다 (패키지 관리자 콘솔에서 다음 명령 실행) Install-Package Microsoft.Owin.Host.SystemWeb
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 await
ed 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.
(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
참고URL : https://stackoverflow.com/questions/22598567/cant-find-request-getowincontext
'code' 카테고리의 다른 글
응용 프로그램 오류 :이 버전의 응용 프로그램은 시장 청구 용으로 구성되지 않았습니다. (0) | 2020.09.02 |
---|---|
특정 시간 PHP 후 페이지 리디렉션 (1) | 2020.09.02 |
Xcode 명령-슬래시 바로 가기가 주석으로 만 작동하는 경우가 있음 (0) | 2020.09.02 |
UIView 레이어의 내부 그림자 효과? (0) | 2020.09.02 |
숫자를 백분율로 (0) | 2020.09.02 |