Can't find Request.GetOwinContext

后端 未结 13 1252
北荒
北荒 2020-12-04 22:47

I have been searching for an hour trying to figure out why this isn\'t working.

I have a ASP.Net MVC 5 application with a WebAPI. I am trying to get Request.GetOwin

相关标签:
13条回答
  • 2020-12-04 23:24

    I had to add package Microsoft.AspNet.Identity.Owin

    0 讨论(0)
  • 2020-12-04 23:25

    (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.

    0 讨论(0)
  • 2020-12-04 23:26

    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;
            }
        }
    
    0 讨论(0)
  • 2020-12-04 23:31

    I have this problem and I download extra package from nuget to solve my problem,(run following command in Package Manager Console) Install-Package Microsoft.Owin.Host.SystemWeb

    0 讨论(0)
  • 2020-12-04 23:34

    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.

    0 讨论(0)
  • 2020-12-04 23:36

    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);`
    
    0 讨论(0)
提交回复
热议问题