Example of using ASP.NET Identity 2.0 UserManagerFactory with UseOAuthBearerTokens method?

asp.net-identity, asp.net-mvc, asp.net-mvc-5, owin

Solution

I am adding stubs here which show you how you can use OAuthBearerTokens... You do not have to use the UserManagerFactory that you were using in SPA. You can switch that to use the PerOWINContext pattern.

Startup.Auth.cs

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

OAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpointPath = new PathString("/Token"),
    Provider = new ApplicationOAuthProvider(PublicClientId),
    AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
    AllowInsecureHttp = true
};

ApplicationOAuthProvider.cs

public ApplicationOAuthProvider(string publicClientId)
{
   if (publicClientId == null)
   {
       throw new ArgumentNullException("publicClientId");
   }
   _publicClientId = publicClientId;
}

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
   var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();

   ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);

   if (user == null)
   {
       context.SetError("invalid_grant", "The user name or password is incorrect.");
       return;
   }

   ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
               OAuthDefaults.AuthenticationType);
   ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
                DefaultAuthenticationTypes.ApplicationCookie);

   AuthenticationProperties properties = CreateProperties(user.UserName);
   AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
   context.Validated(ticket);
   context.Request.Context.Authentication.SignIn(cookiesIdentity); 
}
// namespace below needed to enable GetUserManager extension of the OwinContext
using Microsoft.AspNet.Identity.Owin;

Problem

The ASP.NET Identity 2.0 alpha ships with new middleware to manage getting an instance of the `UserManager` (`app.UseUserManagerFactory` to set this up) and getting an instance of the `DbContext` (`app.UseDbContextFactory` to set this up). There is an example showing how to get this working with an MVC app, but there is no documentation on how to get this working from the SPA template which uses `OAuthBearerTokens`, unlike the sample. I currently am stuck with: ``` UserManagerFactory = () => new DerivedUserManager(new CustomUserStore(new CustomDbContext())); OAuthOptions = new Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/Token"), Provider = new MyApp.Web.Api.Providers.ApplicationOAuthProvider(PublicClientId, UserManagerFactory), AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"), AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), AllowInsecureHttp = true }; app.UseOAuthBearerTokens(OAuthOptions); ``` and have no idea how to replace the `UserManagerFactory` above with calls like these from the 2.0 alpha samples while still working with the `OAuthBearerTokens` objects used in the SPA template: ``` app.UseDbContextFactory(ApplicationDbContext.Create); // Configure the UserManager app.UseUserManagerFactory(new IdentityFactoryOptions<ApplicationUserManager>() { DataProtectionProvider = app.GetDataProtectionProvider(), Provider = new IdentityFactoryProvider<ApplicationUserManager>() { OnCreate = ApplicationUserManager.Create } }); ``` Thanks... -Ben

Original source