Постоянное невосприимчивое удостоверение ASP.NET с шаблонами и ConfigureOAuthTokenGeneration - PullRequest
0 голосов
/ 19 сентября 2019

Я использовал этот шаблон для реализации ASP.NET Identity 2.2 с чистой архитектурой DDD.Тем не мение;когда я хочу использовать ConfigureOAuthTokenGeneration, требуется CreatePerOwinContext, чтобы узнать ваш DbContext и экземпляр UserManager / RoleManager с DbContext.обычно я бы пошел с:

    private void ConfigureOAuthTokenGeneration(IAppBuilder app)
    {
        app.CreatePerOwinContext(AppDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationRoleManager>(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<IdentityRole>
{
    public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
        : base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        var appRoleManager = new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<AppDbContext>()));

        return appRoleManager;
    }
}

UserManager :

public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        var appDbContext = context.Get<AppDbContext>();
        var appUserManager = new ApplicationUserManager(new UserStore<ApplicationUser>(appDbContext));

        return appUserManager;
    }
}

проблема в том, что когда я хочу передать UserManager и RoleManager в CreatePerOwinContext, требуется метод Create, как описано выше, я не знаю, как реализовать их, используя созданный мной RoleStore / UserStore, поскольку их конструктор использует мой IIdentityContext и вышеклассы, я не могу вставить свой IIdentityContext, вот моя реализация RoleStore / UserStore:

RoleStore :

public class RoleStore : IRoleStore<IdentityRole, Guid>,
    IQueryableRoleStore<IdentityRole, Guid>,
    IDisposable
{
    private readonly IIdentityContext _context;
    public RoleStore(IIdentityContext context)
    {
        _context = context;
    }
}

UserStore :

public class UserStore : IUserLoginStore<IdentityUser, Guid>,
    IUserClaimStore<IdentityUser, Guid>,
    IUserRoleStore<IdentityUser, Guid>,
    IUserPasswordStore<IdentityUser, Guid>,
    IUserSecurityStampStore<IdentityUser, Guid>,
    IUserStore<IdentityUser, Guid>, IDisposable
{
    private readonly IIdentityContext _context;

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

}

и My IIdentityContext :

public interface IIdentityContext : IUnitOfWork
{
    IUserRepository Users { get; }
    IRoleRepository Roles { get; }
}
...