相关疑难解决方法(0)

找不到Request.GetOwinContext

我一直在寻找一个小时试图弄清楚为什么这不起作用.

我有一个带有WebAPI的ASP.Net MVC 5应用程序.我正在尝试获取Request.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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从我读过的内容来看,它应该是System.Net.Http的一部分,但我已经包含了它,但它仍然没有解决.Ctrl-Space也没有给我任何intellisense选项.

我在这里错过了什么?

c# asp.net-web-api owin

93
推荐指数
6
解决办法
8万
查看次数

带有OWIN的Web API会为HttpMessageInvoker抛出ObjectDisposedException

我在ASP.NET WebApi2设置中遇到了一些OWIN的困难.重启后的第一个请求导致异常:

    [ObjectDisposedException: Cannot access a disposed object.
    Object name: 'System.Net.Http.HttpMessageInvoker'.]
       System.Net.Http.HttpMessageInvoker.CheckDisposed() +327456
       System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) +24
       System.Web.Http.Owin.<InvokeCore>d__0.MoveNext() +501
       System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
       System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
       Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<RunApp>d__5.MoveNext() +187
       System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
       System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
       Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<DoFinalWork>d__2.MoveNext() +185
       Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +69
       Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +64
       System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +483
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +157
    
我认为这个问题是由于第一次点击EF DbContext时长时间运行的任务造成的.总而言之,第一个请求大约是4-6000毫秒.在第一次请求之后,没有更多例外.

我已经使用webapi项目和以下OWIN启动(并且没有global.asax)以简化形式重现了该问题:

public class Startup
{
    public void Configuration(IAppBuilder app) {
        var config = new HttpConfiguration();
        config.MapHttpAttributeRoutes();

        // ---- simulate long running initialization code -------
        Thread.Sleep(3000);
        // ------------------------------------------------------

        app.UseWebApi(config);
    } …
Run Code Online (Sandbox Code Playgroud)

asp.net owin asp.net-web-api2

9
推荐指数
1
解决办法
4053
查看次数

标签 统计

owin ×2

asp.net ×1

asp.net-web-api ×1

asp.net-web-api2 ×1

c# ×1