I've been using this
pattern for implementing ASP.NET Identity 2.2 with a clean DDD architecture. However; when I want to use ConfigureOAuthTokenGeneration it requires CreatePerOwinContext to know your DbContext and an instance of UserManager/RoleManager with DbContext . normally I would go with:
private void ConfigureOAuthTokenGeneration(IAppBuilder app)
{
app.CreatePerOwinContext(AppDbContext.Create);
app.CreatePerOwinContext(ApplicationUserManager.Create);
app.CreatePerOwinContext(ApplicationRoleManager.Create);
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = false,
TokenEndpointPath = new PathString("/oauth/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new CustomOAuthProvider(),
AccessTokenFormat = new CustomJwtFormat(ConfigurationManager.AppSettings["ApiPath"])
};
app.UseOAuthAuthorizationServer(OAuthServerOptions);
}
RoleManager:
public class ApplicationRoleManager : RoleManager
{
public ApplicationRoleManager(IRoleStore roleStore)
: base(roleStore)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions options, IOwinContext context)
{
var appRoleManager = new ApplicationRoleManager(new RoleStore(context.Get()));
return appRoleManager;
}
}
UserManager:
public class ApplicationUserManager : UserManager
{
public ApplicationUserManager(IUserStore store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context)
{
var appDbContext = context.Get();
var appUserManager = new ApplicationUserManager(new UserStore(appDbContext));
return appUserManager;
}
}
The issue is when I want to pass UserManager and RoleManager to CreatePerOwinContext it requires a Create Method like above,I don't know how to implement them using the RoleStore/UserStore that I've created since their constructor takes my IIdentityContext and within above classes, I can't inject my IIdentityContext here is my Implementation of RoleStore/UserStore:
What I have tried:
RoleStore:
public class RoleStore : IRoleStore,
IQueryableRoleStore,
IDisposable
{
private readonly IIdentityContext _context;
public RoleStore(IIdentityContext context)
{
_context = context;
}
}
UserStore:
public class UserStore : IUserLoginStore,
IUserClaimStore,
IUserRoleStore,
IUserPasswordStore,
IUserSecurityStampStore,
IUserStore, IDisposable
{
private readonly IIdentityContext _context;
public UserStore(IIdentityContext context)
{
_context = context;
}
}
And My IIdentityContext:
public interface IIdentityContext : IUnitOfWork
{
IUserRepository Users { get; }
IRoleRepository Roles { get; }
}