HttpContext GetOwinContext() outside Controller

In AccountController you can find this to access to UserManager

HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

If you need to access to ApplicationRoleManager, ApplicationSignInManager or ApplicationDbContext

HttpContext.GetOwinContext().Get<ApplicationRoleManager>();

This is true if you write this code inside Controller because HttpContext is a property of Controller class:

public HttpContextBase HttpContext { get; }

If you need to access to these manager objects outside the controller

HttpContext is the static class System.Web.HttpContext that is not HttpContextBase class.
The right way is:

HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();

If you don’t find GetOwinContext extension method include this:

using Microsoft.AspNet.Identity.Owin;
Annunci

Informazioni su Andrea Regoli

Project Manager .Net Developer WPF WP7 Asp.Net c# javascript ajax SQL sharepoint
Questa voce è stata pubblicata in Asp.Net, MVC e contrassegnata con , , , , , . Contrassegna il permalink.

Una risposta a HttpContext GetOwinContext() outside Controller

  1. Tomasz Jagusz ha detto:

    Thank You for this! I’ve pulled my hair for two days now and without Your post I’d probably be bald in no time 🙂

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...