What's the best way to keep access to the ApplicationUserManager after the controller has been disposed?
<p>Ran into an interesting problem yesterday. Only my mvc website with Identity 1 I used an <code>IPrincipal</code> extension method to do custom authentication, which worked fine across all the razor views where it was needed. Now with Identity 2, I need to get access in the extension method to the <code>ApplicationUserManager</code>, so I've been forced to pass the <code>HttpContext.Current</code> through from all the views.</p>

<p>This works fine, apart from inside razor <code>@section</code> sections - following the call stack, it appears that these don't get rendered until after the controller has been disposed, which disposes my database and user managers etc.</p>

<p>What would be the best way around this limitation? I want to keep using sections, as they let me easily inject javascript down the very bottom of the html, but I also want to keep using razor extensions inside these sections as they are a very nice, easy way to control what gets outputted to the page.</p>

<p>At the moment I'm being forced to create a new <code>ApplicationUserManager</code> and <code>MyDbContext</code>, stuff them in the <code>ViewBag</code> and then pass them along to the extension, which isn't ideal.</p>

<p>Example of my extension method:</p>

<pre><code>public static bool HasPermission(this IPrincipal source, ApplicationUserManager um, MyDbContext db, string controller, string method) {
        if (!source.Identity.IsAuthenticated) {
            return false;
        }

        var roles = um.GetRoles(source.Identity.GetUserId());
        if (roles.Count == 0) {
            return false;
        }

        // Admins have global access
        string role = roles[0];
        if (role.Equals("Admin")) {
            return true;
        }
        // This is fine being un-domained, permissions are global
        PermissionMatrix permission = db.PermissionMatrices.FirstOrDefault(x =&gt; x.Controller == controller &amp;&amp; x.Action == method &amp;&amp; x.RoleName == role);
        return permission != null;
    }
</code></pre>

<p>Example of where it's used:</p>

<pre><code>@if (User.HasPermission((ApplicationUserManager)ViewBag.UserManager, (MyDbContext)ViewBag.Database, "Event", "Create")) {
    @Html.ActionLink("Create New", "Create")
}
</code></pre>

<p>Editing to make the problem itself more clear:</p>

<p>This was the extension method before I put my hack in -</p>

<pre><code>public static bool HasPermission(this IPrincipal source, HttpContext context, string controller, string method) {
    var userManager = context.GetOwinContext().Get&lt;ApplicationUserManager&gt;();
    var myDb = context.GetOwinContext().Get&lt;MyDbContext&gt;();
</code></pre>

<p>Like I kind of said above, this works (passing the HttpContext.Current through from a .cshtml page), UNLESS the call is inside a razor <code>@section</code>, then it fails, because these run AFTER the controller has been disposed, which disposes the stuff inside the OwinContext.</p>

<p>The code above is the hacky way I've got it working at the moment; in my Controllers I have to create an extra MyDbContext and an extra ApplicationUserManager.</p>