在ASP.NET Web应用程序之外获取UserManager实例?

时间:2018-08-10 19:55:35

标签: c# asp.net asp.net-identity asp.net-identity-2

我有一个带有ASP.NET标识(​​个人帐户)的ASP.NET MVC 5 Web应用程序。但是我需要能够从控制台应用程序注册新用户。

因此,我要将一些ASP.NET Identity类从Web应用程序移到要在Web应用程序和CLI之间共享的类库中。

我已经成功移动了以下内容:

public class PortalDbContext : IdentityDbContext<PortalUser>
{
    public PortalDbContext(string connectionString)
        : base(connectionString, throwIfV1Schema: false)
    {
    }

    public static PortalDbContext Create(string connectionString)
    {
        return new PortalDbContext(connectionString);
    }
}

public class PortalUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<PortalUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        // Add custom user claims here
        return userIdentity;
    }
}

public class PortalUserManager : UserManager<PortalUser>
{
    public PortalUserManager(IUserStore<PortalUser> store) : base(store)
    {
    }

    public async Task<IdentityResult> RegisterUser(string email, string password)
    {
        PortalUser user = new PortalUser { UserName = email, Email = email };

        return await this.CreateAsync(user, password);
    }
}

但是我不知道从何处获得IUserStore<PortalUser>所需的PortalUserManager

在网络应用中,该管理器是从HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>()检索的,我显然不能在类库中使用它。

2 个答案:

答案 0 :(得分:1)

看看OwinRequestScopeContext nuget软件包。它使您可以使用上下文,而无需依赖System.Web。我将在当前自述文件中添加示例,以使它没有仅链接的答案:

# Usage 

// using Owin; you can use UseRequestScopeContext extension method.

// enabled timing is according to Pipeline.
// so I recommend enable as far in advance as possible.
app.UseRequestScopeContext();

app.UseErrorPage();
app.Run(async _ =>
{
    // get global context like HttpContext.Current.
    var context = OwinRequestScopeContext.Current;

    // Environment is raw Owin Environment as IDictionary<string, object>.
    var __ = context.Environment;

    // optional:If you want to change Microsoft.Owin.OwinContext, you can wrap.
    new Microsoft.Owin.OwinContext(context.Environment);

    // Timestamp is request started(correctly called RequestScopeContextMiddleware timing).
    var ___ = context.Timestamp;

    // Items is IDictionary<string, object> like HttpContext.Items.
    // Items is threadsafe(as ConcurrentDictionary) by default.
    var ____ = context.Items;

    // DisposeOnPipelineCompleted can register dispose when request completed(correctly RequestScopeContextMiddleware underling Middlewares finished)
    // return value is cancelToken. If call token.Dispose() then canceled register.
    var cancelToken = context.DisposeOnPipelineCompleted(new TraceDisposable());

    // OwinRequestScopeContext over async/await also ConfigureAwait(false)
    context.Items["test"] = "foo";
    await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
    var _____ = OwinRequestScopeContext.Current.Items["test"]; // foo

    await Task.Run(() =>
    {
        // OwinRequestScopeContext over new thread/threadpool.
        var ______ = OwinRequestScopeContext.Current.Items["test"]; // foo
    });

    _.Response.ContentType = "text/plain";
    await _.Response.WriteAsync("Hello OwinRequestScopeContext! => ");
    await _.Response.WriteAsync(OwinRequestScopeContext.Current.Items["test"] as string); // render foo
});

答案 1 :(得分:0)

我最终向Create(string connectionString)添加了静态方法PortalUserManager,该方法将创建UserStoreDbContext并返回管理器的新实例。

public class PortalDbContext : IdentityDbContext<PortalUser>
{
    public PortalDbContext(string connectionString)
        : base(connectionString, throwIfV1Schema: false)
    {
    }

    public static PortalDbContext Create(string connectionString)
    {
        return new PortalDbContext(connectionString);
    }
}

public class PortalUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<PortalUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        // Add custom user claims here
        return userIdentity;
    }
}

public class PortalUserManager : UserManager<PortalUser>
{
    public PortalUserManager(IUserStore<PortalUser> store) : base(store)
    {
    }

    public static PortalUserManager Create(string connectionString)
    {
        UserStore<PortalUser> userStore = new UserStore<PortalUser>(PortalDbContext.Create(connectionString));

        PortalUserManager manager = new PortalUserManager(userStore);

        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator<PortalUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = true,
            RequireUniqueEmail = true                
        };

        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        return manager;
    }

    public async Task<IdentityResult> RegisterUser(string email, string password)
    {
        PortalUser user = new PortalUser { UserName = email, Email = email };

        return await this.CreateAsync(user, password);
    }
}