如何将IdentityManager与AspNetCoreIdentity一起使用

时间:2016-09-11 19:26:06

标签: asp.net-core asp.net-identity identitymanager

是否有机会使用带有AspNet核心标识的IdentityManager?

我没有找到任何针对AspNetCoreidentity的IIdentityManagerService的实现。

或者,使用AspnetCore身份时是否有任何IdentityManager的替换?

谢谢。

1 个答案:

答案 0 :(得分:1)

是的,您可以使用IdentityManager管理您的AspNetIdentity用户。

IdentityManager本身有一个名为IdentityManager.AspNetIdentity的实现。您可能需要阅读an article describing your scenario by Scott Hanselman

您基本上想要做的是加载继承自AspNetIdentityManagerService

的IdentityManagerService
public class ApplicationIdentityManagerService : AspNetIdentityManagerService<ApplicationUser, string, IdentityRole, string>
{
    public ApplicationIdentityManagerService(ApplicationUserManager userMgr, ApplicationRoleManager roleMgr) : base(userMgr, roleMgr)
    {
        // Nothing
    }
}

然后您可以将其插入工厂:

app.Map("/users", idm =>
{
    var factory = new IdentityManagerServiceFactory();
    factory.IdentityManagerService = new Registration<IIdentityManagerService, ApplicationIdentityManagerService>();
    factory.Register(new Registration<ApplicationUserManager>());
    factory.Register(new Registration<ApplicationUserStore>());
    factory.Register(new Registration<ApplicationRoleManager>());
    factory.Register(new Registration<ApplicationRoleStore>());
    factory.Register(new Registration<ApplicationDbContext>(resolver => new ApplicationDbContext("dbase")));
    factory.Register(new Registration<ApplicationDbContext>());

    idm.UseIdentityManager(new IdentityManagerOptions { 
        Factory = factory
    });
});

more complete example上找到IdentityManager AspNetIdentity GitHub repo