本文翻译自:ASP.NET Identity - HttpContext has no extension method for GetOwinContext
I have downloaded, and successfully ran the ASP.NET Identity sample from here: https://github.com/rustd/AspnetIdentitySample 我已经从这里下载并成功运行了ASP.NET Identity示例: https : //github.com/rustd/AspnetIdentitySample
I am now in the middle of implementing the ASP.NET Identity framework in my project and have ran into a problem, that has driven me mad all day... 我现在正在我的项目中实现ASP.NET Identity框架,并遇到了一个问题,这让我整天都疯了......
GetOwinContext()
does not exist as an extension method on myHttpContext
GetOwinContext()
在我的HttpContext
上不作为扩展方法存在
I am implementing the identity framework in class library. 我正在类库中实现身份框架。 I have installed all the latest (pre-release version) of the Identity framework and everything - apart from this - is working fine. 我已经安装了Identity框架的所有最新版本(预发布版本),除此之外的所有内容都正常工作。
I have tried implementing the same code as the same direct in my controller, and find the same problem. 我尝试在我的控制器中实现与直接相同的代码,并找到相同的问题。
I'm clearly missing a reference somewhere, though I have no idea what..!.. 我显然错过了某个地方的参考,虽然我不知道是什么..!..
The code-block that is killing me is: 杀死我的代码块是:
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
I have added references to the following - tried these both in my class library and also direct on the controller, none of them work for me... 我添加了对以下内容的引用 - 在我的类库中尝试了这些并且也直接在控制器上,它们都不适合我...
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using Microsoft.Owin;
using System.Web;
... this is driving me up the wall....any idea? ......这让我起了墙......任何想法?
UPDATE UPDATE
I have checked the versions of Identity & OWIN in the sample, and I have made sure I have the same versions in my solution. 我已经检查了示例中Identity和OWIN的版本,并确保我的解决方案中有相同的版本。
More so, if I search the object browser on the sample for GetOwinContext
I can find the method, however when I search for it in my solution it is nowhere to be found... I must have some library out of date, but I can't find it! 更重要的是,如果我在GetOwinContext
的示例中搜索对象浏览器,我可以找到该方法,但是当我在我的解决方案中搜索它时,它无处可寻......我必须有一些库过时,但我可以找不到它!
#1楼
参考:https://stackoom.com/question/1Qjc9/ASP-NET标识-HttpContext没有GetOwinContext的扩展方法
#2楼
I believe you need to reference the current HttpContext
if you are outside of the controller. 我相信如果你在控制器之外,你需要引用当前的HttpContext
。 The MVC controllers have a base reference to the current context. MVC控制器具有对当前上下文的基本引用。 However, outside of that, you have to explicitly declare you want the current HttpContext
但是,除此之外,您必须显式声明您想要当前的HttpContext
return HttpContext.Current.GetOwinContext().Authentication;
As for it not showing up, a new MVC 5 project template using the code you show above (the IAuthenticationManager
) has the following using statements at the top of the account controller: 至于它没有显示,使用您在上面显示的代码( IAuthenticationManager
)的新MVC 5项目模板在帐户控制器的顶部具有以下使用语句:
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using WebApplication2.Models;
Commenting out each one, it appears the GetOwinContext()
is actually a part of the System.Web.Mvc assembly. 注释掉每一个,看起来GetOwinContext()
实际上是System.Web.Mvc程序集的一部分。
#3楼
Make sure you installed the nuget package Microsoft.AspNet.Identity.Owin
. 确保安装了nuget包Microsoft.AspNet.Identity.Owin
。 Then add System.Net.Http
namespace. 然后添加System.Net.Http
命名空间。
#4楼
ARGH! 哎呀!
I found it ... I didn't have an extra package, called Microsoft.Owin.Host.SystemWeb
我找到了 ...我没有一个名为Microsoft.Owin.Host.SystemWeb
的额外软件包
Once i searched and installed this, it worked. 一旦我搜索并安装了它,它就有效了。
Now - i am not sure if i just missed everything, though found NO reference to such a library or package when going through various tutorials. 现在 - 我不确定我是否只是错过了所有内容,但在浏览各种教程时发现没有参考这样的库或包。 It also didn't get installed when i installed all this Identity framework... Not sure if it were just me.. 当我安装所有这个Identity框架时它也没有安装......不确定它是否只是我...
EDIT Although it's in the Microsoft.Owin.Host.SystemWeb
assembly it is an extension method in the System.Web
namespace, so you need to have the reference to the former, and be using
the latter. 编辑虽然它在Microsoft.Owin.Host.SystemWeb
程序集中,但它是System.Web
命名空间中的扩展方法,因此您需要引用前者,并using
后者。
#5楼
After trial and error comparing the using statements of my controller and the Asp.Net Template controller 在尝试和错误比较我的控制器和Asp.Net模板控制器的using语句之后
using System.Web;
Solved the problem for me. 解决了我的问题。 You are also going to need to add: 您还需要添加:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
To use GetUserManager method. 使用GetUserManager方法。
Microsoft couldn't find a way to resolve this automatically with right click and resolve like other missing using statements? Microsoft无法通过右键单击自动解决此问题,并像其他缺少使用语句一样解决?
#6楼
我拥有所有正确的包和使用,但必须先构建才能使GetOwinContext()
工作。