empz
empz

Reputation: 11788

Get the UserManager instance outside of ASP.NET web application?

I have an ASP.NET MVC 5 web app with ASP.NET Identity (Individual Accounts). But I need to be able to register new users from a console app.

So I'm moving some of the ASP.NET Identity classes from the web app into a class library to be shared between the web app and the CLI.

I have successfully moved the following:

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);
    }
}

But I have no idea where to get the IUserStore<PortalUser> the PortalUserManager needs from.

In the web app, this manager is retrieved from HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>() which I clearly can't use in a class library.

Upvotes: 3

Views: 1044

Answers (2)

empz
empz

Reputation: 11788

I've ended up adding a static method Create(string connectionString) to PortalUserManager that will create the UserStore and DbContext and return a new instance of the manager.

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);
    }
}

Upvotes: 0

John H
John H

Reputation: 14655

Take a look at the OwinRequestScopeContext nuget package. It allows you to make use of a context without a dependency on System.Web. I'll add the example from the current readme for the sake of not having a link-only answer:

# 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
});

Upvotes: 1

Related Questions