Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

2020IP/TwentyTwenty.IdentityServer3.AspNetIdentity3

Repository files navigation

20|20 IdentityServer3.AspNetIdentity3

Deprecated: This repository is no longer in active development or maintenance.

AspNet Identity 3 user management plugin for IdentityServer v3

NuGet

####Usage The following usage uses a Guid as the Key type.

To provide customizability, creating custom classes is recommened.

public class IdentityContext : IdentityDbContext<User, Role, Guid>
{
	public IdentityContext() : base() { }        
}

public class UserStore : UserStore<User, Role, IdentityContext, Guid>
{
	public UserStore(IdentityContext context) : base(context) { }
}

public class UserManager : UserManager<User>
{
	public UserManager(UserStore store, IOptions<IdentityOptions> options, IPasswordHasher<User> hasher,
		IEnumerable<IUserValidator<User>> userValidators, IEnumerable<IPasswordValidator<User>> passwordValidators,
		ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<User>> logger,
		IHttpContextAccessor contextAccessor) 
		: base(store, options, hasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger, contextAccessor)
	{
	}
}

public class Role : IdentityRole<Guid> { }

public class User : IdentityUser<Guid>
{
	public override Guid Id { get; set; } = Guid.NewGuid();
}

Create a custom user service

public class UserService : AspNetIdentity3Plugin<User, Guid>
{
	public UserService(UserManager userMgr) 
		: base(userMgr, new AspNetIdentityPluginOptions())
	{
	}
}

NOTE: In the UserService class you will most likely need to override the following methods to fit your custom implementation: UpdateAccountFromExternalClaimsAsync InstantiateNewUserFromExternalProviderAsync TryGetExistingUserFromExternalProviderClaimsAsync

In the Startup.cs register your Identity Context with Entity Framework, register your custom services and add Identity to the services

public void ConfigureServices(IServiceCollection services)
{
	...
	services.AddEntityFramework()
		.AddSqlServer()
		.AddDbContext<IdentityContext>(o => o.UseSqlServer(connectionString));
		
	services.AddScoped<IUserService, UserService>()
		.AddScoped<UserManager>()
		.AddScoped<UserStore>();
		
	services.AddIdentity<User, Role>();
	...
}

Configure the IdentityServerServiceFactory to use the user service and to use Identity

public void Configure(IApplicationBuilder app)
{
	...
    var factory = new IdentityServerServiceFactory();
    factory.RegisterUserServices(app.ApplicationServices);

    owinAppBuilder.UseIdentityServer(new IdentityServerOptions
    {
        ...
        Factory = factory,
        ...
    });
	
	app.UseIdentity();
    ...
}

About

AspNet Identity 3 user management plugin

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published