无法找到Request.GetOwinContext。

13 浏览
0 Comments

无法找到Request.GetOwinContext。

我已经搜索了一个小时,试图弄清楚为什么这没用。

我有一个ASP.Net MVC 5应用程序和一个WebAPI。我正在尝试获取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也没有给我任何智能感知选项。

我在这里错过了什么?

admin 更改状态以发布 2023年5月24日
0
0 Comments

对我来说,这些方法都没有起作用。我不得不将使用Identity创建的Nuget包与其他进行比较,我发现我缺少了一个Nuget包,添加后问题解决了

Microsoft.Owin.Host.SystemWeb

显然,如果没有它,您需要在使用ASP.NET请求管道时,在IIS上运行OWIN(意思是你没有它就无法完成工作!)

0
0 Comments

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的管道非常不同,但通常具有相同的命名约定。因此,一个命名不同的扩展方法不适用于另一个。同样,许多操作过滤器等也是如此。

0