| FirstName = user.FirstName, | FirstName = user.FirstName, | ||||
| LastName = user.LastName, | LastName = user.LastName, | ||||
| Token = token, | Token = token, | ||||
| RefreshToken = token | |||||
| RefreshToken = token, | |||||
| Role = (await _userManager.GetRolesAsync(user))[0] | |||||
| }; | }; | ||||
| return new ServiceResponseDTO<AuthenticateResponseDto> | return new ServiceResponseDTO<AuthenticateResponseDto> | ||||
| LastName = userk.LastName, | LastName = userk.LastName, | ||||
| Username = userk.UserName, | Username = userk.UserName, | ||||
| Token = model.Token, | Token = model.Token, | ||||
| RefreshToken = model.RefreshToken | |||||
| RefreshToken = model.RefreshToken, | |||||
| Role = (await _userManager.GetRolesAsync(userk))[0] | |||||
| } | } | ||||
| }; | }; | ||||
| } | } | ||||
| LastName = userk.LastName, | LastName = userk.LastName, | ||||
| Username = userk.UserName, | Username = userk.UserName, | ||||
| Token = token, | Token = token, | ||||
| RefreshToken = token | |||||
| RefreshToken = token, | |||||
| Role = (await _userManager.GetRolesAsync(userk))[0] | |||||
| } | } | ||||
| }; | }; | ||||
| } | } |
| { | { | ||||
| _logger.LogInformation("Start getting all users"); | _logger.LogInformation("Start getting all users"); | ||||
| _logger.LogInformation("Getting data from DB"); | _logger.LogInformation("Getting data from DB"); | ||||
| var fromDb = await _userManager.Users.ToListAsync(); | |||||
| var fromDb = await _userManager.GetUsersInRoleAsync("Admin"); | |||||
| _logger.LogInformation($"Received {fromDb.Count} ads from db."); | _logger.LogInformation($"Received {fromDb.Count} ads from db."); | ||||
| return fromDb; | return fromDb; | ||||
| } | } | ||||
| }; | }; | ||||
| await _userManager.CreateAsync(user, StringGenerator.GenerateRandomPassword()); | await _userManager.CreateAsync(user, StringGenerator.GenerateRandomPassword()); | ||||
| await _userManager.AddToRoleAsync(user, "Admin"); | |||||
| // generate invitation token for user | // generate invitation token for user | ||||
| // encoded for URLs | // encoded for URLs |
| public string Username { get; set; } | public string Username { get; set; } | ||||
| public string Token { get; set; } | public string Token { get; set; } | ||||
| public string RefreshToken { get; set; } | public string RefreshToken { get; set; } | ||||
| public string Role { get; set; } | |||||
| } | } | ||||
| } | } |
| using Microsoft.AspNetCore.Identity; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Data | |||||
| { | |||||
| public class DataSeeder | |||||
| { | |||||
| public static async void Seed(IServiceProvider serviceProvider) | |||||
| { | |||||
| using var scope = serviceProvider.CreateScope(); | |||||
| var databaseContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>(); | |||||
| var userManager = scope.ServiceProvider.GetRequiredService<UserManager<User>>(); | |||||
| if (!databaseContext.Users.Any()) | |||||
| { | |||||
| var superAdmin = new User | |||||
| { | |||||
| AccessFailedCount = 0, | |||||
| Email = "admin@dilig.net", | |||||
| FirstName = "SuperAdmin", | |||||
| LastName = "SuperAdmin", | |||||
| UserName = "superAdmin", | |||||
| EmailConfirmed = true | |||||
| }; | |||||
| await userManager.CreateAsync(superAdmin, "Nekasifra123!"); | |||||
| await userManager.AddToRoleAsync(superAdmin, "SuperAdmin"); | |||||
| await databaseContext.SaveChangesAsync(); | |||||
| } | |||||
| } | |||||
| } | |||||
| } |
| public static class ServiceCollection | public static class ServiceCollection | ||||
| { | { | ||||
| public static void ConfigureData(this IServiceCollection services, IConfiguration configuration) | |||||
| public static async void ConfigureData(this IServiceCollection services, IConfiguration configuration) | |||||
| { | { | ||||
| services.AddDbContext<DatabaseContext>(options => | services.AddDbContext<DatabaseContext>(options => | ||||
| { | { |
| /// <summary> | /// <summary> | ||||
| /// Services configuration | /// Services configuration | ||||
| /// </summary> | /// </summary> | ||||
| public static void ConfigureData(this WebApplicationBuilder builder) | |||||
| public static async void ConfigureData(this WebApplicationBuilder builder) | |||||
| { | { | ||||
| IServiceCollection services = builder.Services; | IServiceCollection services = builder.Services; | ||||
| services.ConfigureData(builder.Configuration); | services.ConfigureData(builder.Configuration); | ||||
| /// <summary> | /// <summary> | ||||
| /// App configuration | /// App configuration | ||||
| /// </summary> | /// </summary> | ||||
| public static void ConfigureData(this WebApplication app) | |||||
| public async static void ConfigureData(this WebApplication app) | |||||
| { | { | ||||
| // | |||||
| using (var serviceScope = app.Services.CreateScope()) | |||||
| { | |||||
| var roleManager = serviceScope.ServiceProvider.GetRequiredService<RoleManager<AppRole>>(); | |||||
| if (!await roleManager.RoleExistsAsync("SuperAdmin")) | |||||
| { | |||||
| var managerRole = new AppRole("SuperAdmin"); | |||||
| await roleManager.CreateAsync(managerRole); | |||||
| } | |||||
| if (!await roleManager.RoleExistsAsync("Admin")) | |||||
| { | |||||
| var managerRole = new AppRole("Admin"); | |||||
| await roleManager.CreateAsync(managerRole); | |||||
| } | |||||
| } | |||||
| DataSeeder.Seed(app.Services); | |||||
| } | } | ||||
| } | } |