ASP.NET Identity IdentityManagerService · IdentityManager/IdentityManager Wiki · GitHub
Skip to content
This repository was archived by the owner on Feb 15, 2018. It is now read-only.

ASP.NET Identity IdentityManagerService

Brock Allen edited this page Mar 16, 2015 · 9 revisions

IdentityManager for ASP.NET Identity

IdentityManager.AspNetIdentity ASP.NET Identity is an IdentityManagerService implementation for IdentityManager that uses ASP.NET Identity as the identity management system.

The code repository and sample host is available here.

Installing

You can either clone the code and sample host from the repository above, or you can install the NuGet via:

PM> Install-Package IdentityManager.AspNetIdentity -Pre 

ASP.NET Identity IdentityManagerService

The AspNetIdentityManagerService is an implementation of IIdentityManagerService and is designed to integrate with ASP.NET Identity.

It is a generic class and expects generic arguments for the user class its primary key type, and the role class and its primary key type. The constructor then accepts a UserManager and a RoleManager.

Metadata

The AspNetIdentityManagerService can automatically generate the necessary metadata to drive IdentityManager for the standard properties used in ASP.NET Identity such as username, password, email and phone. In addition, there is a constructor parameter includeAccountProperties (which defaults to true) that will also generate the metadata for the properties of the user class being used.

If more customization is needed, then there is an overloaded constructor that provides a delegate to create the metadata. Within this delegate it's possible to use the AspNetIdentityManagerService.GetStandardMetadata to create the default metadata which can then be customized as needed.

Service factory

To integrate into the IdentityManagerConfiguration, a factory that instantiates the AspNetIdentityManagerService is needed, as such:

public class AspNetIdentityIdentityManagerFactory
{
    string connString;

    public AspNetIdentityIdentityManagerFactory(string connString)
    {
        this.connString = connString;
    }

    public IIdentityManagerService Create()
    {
        var db = new IdentityDbContext<IdentityUser>(connString);
        var userStore = new UserStore<IdentityUser>(db);
        var userMgr = new Microsoft.AspNet.Identity.UserManager<IdentityUser>(userStore);
        var roleStore = new RoleStore<IdentityRole>(db);
        var roleMgr = new Microsoft.AspNet.Identity.RoleManager<IdentityRole>(roleStore);

        var svc = new Thinktecture.IdentityManager.AspNetIdentity.AspNetIdentityManagerService<IdentityUser, string, IdentityRole, string>(userMgr, roleMgr);


        return new DisposableIdentityManagerService(svc, db);
    }
}

Since this factory function is invoked per-HTTP request into IdentityManager, if the returned IIdentityManagerService also implements IDisposable then it will be invoked once the request is complete. The DisposableIdentityManagerService is used to wrap the AspNetIdentityManagerService and call Dispose on the database context class.

Startup

To then configure the factory with the IdentityManagerConfiguration and with Katana, this code would be needed in Startup:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var factory = new Thinktecture.IdentityManager.Host.AspNetIdentityIdentityManagerFactory("AspId");
        app.UseIdentityManager(new IdentityManagerConfiguration()
        {
            IdentityManagerFactory = factory.Create
        });
    }
}

Clone this wiki locally