Get DataProtectionProvider in MVC 5 for dependecy injection correctly
<p>When trying to create a <code>DataProtectionProvider</code> manually I have stumbled upon the Microsoft documenation to <a href="https://msdn.microsoft.com/en-us/library/microsoft.owin.security.dataprotection.dpapidataprotectionprovider(v=vs.113).aspx" rel="noreferrer"><code>DpapiDataProtectionProvider</code></a> which says:</p>

<blockquote>
  <p>Used to provide the data protection services that are derived from the
  Data Protection API. <strong>It is the best choice of data protection when you
  application is not hosted by ASP.NET</strong> and all processes are running as
  the same domain identity.</p>
</blockquote>

<p>A question suddenly arises: <strong>What is the best choice when your application IS hosted by ASP.NET?</strong></p>

<p>Searching further, it seems the best choice is to obtain the <code>DataProtectionProvider</code> from OWIN. That can be done in Startup configuration, where you have <code>IAppBuilder</code> and using <code>AppBuilderExtensions</code> located in <code>Microsoft.Owin.Security.DataProtection</code> namespace you can call <code>app.GetDataProtectionProvider()</code>.</p>

<p>So far, I am quite satisfied. However, now you want to inject the <code>DataProtectionProvider</code> in a constructor of your class (e.g. a <code>UserManager</code>). I have seen <a href="https://stackoverflow.com/questions/27471363/no-iusertokenprovider-is-registered-when-using-structuremap-dependency-injecti">one suggestion</a> where you store the <code>DataProtectionProvider</code> in a static property and then use it where you need, but that seems like a rather wrong solution.</p>

<p>I think a solution similar to the following piece of code would be appropriate (using ninject container):</p>

<pre><code>kernel.Bind&lt;IDataProtectionProvider&gt;()
    // beware, method .GetDataProtectionProvider() is fictional
    .ToMethod(c =&gt; HttpContext.Current.GetOwinContext().GetDataProtectionProvider())
    .InRequestScope();
</code></pre>