Click here to Skip to main content
15,915,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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:

C#
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:

C#
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:

C#
public class RoleStore : IRoleStore,
    IQueryableRoleStore,
    IDisposable
{
    private readonly IIdentityContext _context;
    public RoleStore(IIdentityContext context)
    {
        _context = context;
    }
}

UserStore:

C#
public class UserStore : IUserLoginStore,
    IUserClaimStore,
    IUserRoleStore,
    IUserPasswordStore,
    IUserSecurityStampStore,
    IUserStore, IDisposable
{
    private readonly IIdentityContext _context;

    public UserStore(IIdentityContext context)
    {
        _context = context;
    }

}

And My IIdentityContext:

C#
public interface IIdentityContext : IUnitOfWork
{
    IUserRepository Users { get; }
    IRoleRepository Roles { get; }
}
Posted
Updated 19-Sep-19 5:21am
v2

1 solution

Try something like this:

Change your ApplicationRoleManager and ApplicationUserManager classes to use DI instead of a factory method:
C#
public class ApplicationRoleManager : RoleManager
{
    public ApplicationRoleManager(IRoleStore roleStore) : base(roleStore)
    {
    }
}

public class ApplicationUserManager : UserManager
{
    public ApplicationUserManager(IUserStore store) : base(store)
    {
    }
}
Ensure that they are registered with your DI container:
C#
public static void RegisterComponents()
{
    var container = new UnityContainer();
    ...
    
    container.RegisterType<ApplicationRoleManager>(new TransientLifetimeManager());
    container.RegisterType<ApplicationUserManager>(new TransientLifetimeManager());

    DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
Change the CreatePerOwinContext calls to resolve the instances using the DI container:
C#
private void ConfigureOAuthTokenGeneration(IAppBuilder app)
{
    app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>());
    app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationRoleManager>());
    ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900