ASP.NET標識 - HttpContext沒有GetOwinContext的擴展方法

本文翻譯自: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 my HttpContext 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()工作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章