diff --git a/Idone/Idone.Back/Startup.cs b/Idone/Idone.Back/Startup.cs index fa64200..42eb535 100644 --- a/Idone/Idone.Back/Startup.cs +++ b/Idone/Idone.Back/Startup.cs @@ -36,7 +36,7 @@ public void ConfigureServices(IServiceCollection services) services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).Services .AddIdoneIdentity() .AddIdoneDb(connString) - .AddSecurityDi(); + .AddSecurityDi(Configuration); } } } \ No newline at end of file diff --git a/Idone/Idone.Back/appsettings.Development.json b/Idone/Idone.Back/appsettings.Development.json index 5e66f3b..93bea0c 100644 --- a/Idone/Idone.Back/appsettings.Development.json +++ b/Idone/Idone.Back/appsettings.Development.json @@ -8,5 +8,8 @@ }, "ConnectionStrings": { "default": "Server=(localdb)\\mssqllocaldb;Database=Idone;Trusted_Connection=True;ConnectRetryCount=0;" + }, + "ActiveDirectory": { + "domain": "tomskasu" } } diff --git a/Idone/Idone.Security/SecurityApp.cs b/Idone/Idone.Security/SecurityApp.cs index 8d2c304..144f608 100644 --- a/Idone/Idone.Security/SecurityApp.cs +++ b/Idone/Idone.Security/SecurityApp.cs @@ -2,6 +2,7 @@ { using Idone.Security.Services; + using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; /// @@ -14,10 +15,13 @@ public static class SecurityApp /// /// Сервисы. /// Возвращает сервисы. - public static IServiceCollection AddSecurityDi(this IServiceCollection services) + public static IServiceCollection AddSecurityDi(this IServiceCollection services, IConfiguration config) { + //TODO: при разрастании параметров выделить в отдельный класс десериализации настроек + var adDomain = config.GetSection("ActiveDirectory").GetSection("domain").Value; + services.AddScoped(); - services.AddScoped(); + services.AddScoped(s => new AdService(adDomain)); return services; } diff --git a/Idone/Idone.Security/Services/AdService.cs b/Idone/Idone.Security/Services/AdService.cs index d49861a..c3c0294 100644 --- a/Idone/Idone.Security/Services/AdService.cs +++ b/Idone/Idone.Security/Services/AdService.cs @@ -1,8 +1,10 @@ namespace Idone.Security.Services { + using System; using System.Collections.Generic; using System.DirectoryServices.AccountManagement; using System.Linq; + using System.Net; using Idone.DAL.Dictionaries; using Idone.DAL.DTO; @@ -18,7 +20,28 @@ namespace Idone.Security.Services /// internal class AdService { - private const string DOMAIN = "tomskasu"; + /// + /// Домен сервиса Active Directory. + /// + private readonly string _domain; + + /// + /// Инициализировать зависимости. + /// + /// Домен сервиса Active Directory. + public AdService(string domain) + { + if (string.IsNullOrEmpty(domain)) + { + throw new NullReferenceException($"Пустой аргумент {nameof(domain)}"); + } + if (!Dns.GetHostAddresses(domain).Any()) + { + var msg = $"Не найден домен сервиса Active Directory для переданного аргумента {nameof(domain)}"; + throw new ArgumentException(msg); + } + _domain = domain; + } /// /// Найти пользователей по отображаемому имени. @@ -27,8 +50,7 @@ internal class AdService /// Возращает монаду с найденными совпадениями пользователей по отображаемому имени. public Either> FindUsersByDisplayName(string searchExpression) { - //TODO: вынести домен в настройки - using (var ctx = new PrincipalContext(ContextType.Domain, DOMAIN)) + using (var ctx = new PrincipalContext(ContextType.Domain, _domain)) using (var query = new UserPrincipal(ctx) { DisplayName = searchExpression @@ -57,7 +79,7 @@ internal class AdService /// Результат операции. public Either CreateUser(DtoNewAdUser newUser) { - using (var ctx = new PrincipalContext(ContextType.Domain, DOMAIN)) + using (var ctx = new PrincipalContext(ContextType.Domain, _domain)) using (var query = new UserPrincipal(ctx)) { query.SamAccountName = newUser.Nickname; diff --git a/Idone/Idone.Tests/appsettings.Development.json b/Idone/Idone.Tests/appsettings.Development.json index 77f9978..d04f5e0 100644 --- a/Idone/Idone.Tests/appsettings.Development.json +++ b/Idone/Idone.Tests/appsettings.Development.json @@ -8,5 +8,8 @@ }, "ConnectionStrings": { "default": "Server=172.17.0.3,1433;Database=Idone.Tests;User ID=SA;pwd=;Trusted_Connection=False;" + }, + "ActiveDirectory": { + "domain": "tomskasu" } -} +} \ No newline at end of file