-
Notifications
You must be signed in to change notification settings - Fork 0
Identity
Arsenty Politov edited this page Mar 15, 2019
·
1 revision
Provides:
- Base User and Role classes, that use Guid as id property, instead of String used by default in IdentityUser and IdentityRole.
- Following services:
- IAuthenticatedUserAccessorService - provides an ability to get a domain entity, that represents authenticated user.
- IAuthenticatedUserIdAccessorService - provides an ability to get authenticated user id.
- IAuthenticationStatusService - provides an ability to check whether user is authenticated or not.
- IPrincipalUserAccessorService - provides an ability to get claims principal that represents authenticated user.
- IIdentityKeyDecoder - provides an ability to decode user id from claims principal.
- ISecureCodeGenerationService - provides an ability to generate codes or passwords using cryptographic RNG.
- Default implementation of the above services.
- An extension to database seeder that provides ability to seed database with required user and roles.
- DevGuild.AspNetCore.Services.Identity
To add necessary services, modify Configure method of Startup class in the following way:
- If the project is configured with an application user class with String as identifier, add call of extension method AddStateServicesStringKey to the AddIdentity call.
- If the project is configured with an application user class with Guid as identifier, add call of extension method AddStateServicesGuidKey to the AddIdentity call.
services.AddIdentity<ApplicationUser, Role>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddStateServicesGuidKey<ApplicationUser>();To initialize necessary roles and default users (e.g. admin user), following extensions for database seeder are provided: SeedRoleAsync and SeedUserAsync.
public class ApplicationDbSeed : DbSeed<ApplicationDbContext>
{
public ApplicationDbSeed(IServiceProvider serviceProvider)
: base(serviceProvider)
{
}
public override async Task SeedAsync()
{
await this.Context.SeedRoleAsync<ApplicationDbContext, Role, Guid>(roleName: "Administrator");
await this.Context.SeedRoleAsync<ApplicationDbContext, Role, Guid>(roleName: "User");
await this.Context.SeedUserAsync<ApplicationDbContext, ApplicationUser, Role, Guid>(
userName: "admin",
email: null,
password: "DEFAULT_PASSWORD",
roles: "Administrator");
}
}public async Task Example1(
IAuthenticationStatusService authStatus,
IAuthenticatedUserAccessorService<ApplicationUser> userAccessor,
IAuthenticatedUserIdAccessorService<Guid> userIdAccessor,
IPrincipalUserAccessorService principalAccessor)
{
Boolean authenticated = await authStatus.GetAuthenticationStatusAsync();
if (authenticated)
{
// Will return null if user is not authenticated.
ApplicationUser user = await userAccessor.GetUserAsync();
// Will throw exception if user is not authenticated.
Guid userId = await userIdAccessor.GetUserIdAsync();
// Will return null if user is not authenticated.
ClaimsPrincipal principal = await principalAccessor.GetPrincipalUserAsync();
}
}public async Task Example2(ISecureCodeGenerationService secureCodeGen)
{
// Generate 20-characters code that consists of upper and lower case letters and digits.
String alphaNumeric = secureCodeGen.GenerateAlphaNumericCode(size: 20);
// Generate 10-digits numeric code.
String numeric = secureCodeGen.GenerateNumericCode(size: 10);
// Generate 16-characters code that consists of specified characters.
String upper = secureCodeGen.GenerateCustomCode(size: 16, characters: "ABCDEF");
}