I'm implementing ASP.Net Identity 2 in a WebApi system. To manage email confirmation for new accounts, I had to create a custom ApplicationUserManager
and register it so that it would be created for every request:
public class IdentityConfig{
public static void Initialize(IAppBuilder app)
{
[snip]
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
}
It works correctly inside an ApiController like this:
public class AccountController : ApiController
{
public ApplicationUserManager UserManager
{
get
{
return HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
}
The problem I'm facing is that the ApplicationUserManager.Create
method is not being called before I try to access it in the OAuth Token creation method:
public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var mgr = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
In the above code, mgr is null because GetUserManager retrieves null
Is the token creation method somehow earlier in the pipeline such that the CreatePerOwinContext
methods haven't beeen called yet? If so, what's the best way cache an ApplicationUserManager
so that it can be used inside GrantResourceOwnerCredentials
?