Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV-1 init Авторизации приложения в домен для доступа к информации о принтерах в веб-приложении #2

Merged
merged 10 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions PrinterMonitor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ASP", "src\PresentationASP\
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Presentations", "Presentations", "{923E7B9C-EDE6-4BC4-A561-4FDB159A2DDC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Application", "src\Application\Application.csproj", "{39C3BCBB-94F9-4954-B9DE-2B82DDFC5F77}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Application", "src\Application\Application.csproj", "{39C3BCBB-94F9-4954-B9DE-2B82DDFC5F77}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Domain", "src\Domain\Domain.csproj", "{73940C84-32D9-4548-B5D8-0790BD271F63}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Domain", "src\Domain\Domain.csproj", "{73940C84-32D9-4548-B5D8-0790BD271F63}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infrastructure", "src\Infrastructure\Infrastructure.csproj", "{B15824D0-C294-46BD-BFDD-FD75439F36EF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Infrastructure", "src\Infrastructure\Infrastructure.csproj", "{B15824D0-C294-46BD-BFDD-FD75439F36EF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cross-Cutting", "src\Cross-Cutting\Cross-Cutting.csproj", "{2869E815-7ABF-422F-B59D-DA47C73C1D62}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cross-Cutting", "src\Cross-Cutting\Cross-Cutting.csproj", "{2869E815-7ABF-422F-B59D-DA47C73C1D62}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Элементы решения", "Элементы решения", "{0EE2584E-067F-45AB-A957-3C0FC2A66887}"
ProjectSection(SolutionItems) = preProject
README.MD = README.MD
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{9C52CF31-4A61-46ED-91DE-C73B07D9EE9F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTest", "tests\UnitTest\UnitTest.csproj", "{CA325FB5-C970-4E48-B29B-A2C5631A6AD4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -46,12 +50,17 @@ Global
{2869E815-7ABF-422F-B59D-DA47C73C1D62}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2869E815-7ABF-422F-B59D-DA47C73C1D62}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2869E815-7ABF-422F-B59D-DA47C73C1D62}.Release|Any CPU.Build.0 = Release|Any CPU
{CA325FB5-C970-4E48-B29B-A2C5631A6AD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CA325FB5-C970-4E48-B29B-A2C5631A6AD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA325FB5-C970-4E48-B29B-A2C5631A6AD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CA325FB5-C970-4E48-B29B-A2C5631A6AD4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F7F053B0-8E7F-41C8-A60F-790061386B0D} = {923E7B9C-EDE6-4BC4-A561-4FDB159A2DDC}
{CA325FB5-C970-4E48-B29B-A2C5631A6AD4} = {9C52CF31-4A61-46ED-91DE-C73B07D9EE9F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8AC27A90-8AD6-4D17-9B63-91E4D30A9DE1}
Expand Down
6 changes: 5 additions & 1 deletion src/Application/Application.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>ProgGym.PrinterMonitor.Application</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.DirectoryServices" Version="7.0.1" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Application/Interfaces/ISearchDeviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace ProgGym.PrinterMonitor.Application.Interfaces
{
public interface ISearchDeviceService
{

public Task<List<string>> GetPrinters();
}
}
7 changes: 6 additions & 1 deletion src/Application/MonitorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace ProgGym.PrinterMonitor.Application
{
public class MonitorSettings
{
public string? Subnet { get; set; }
public string? DomainPath { get; set; }

public string? DomainUserName { get; set; }

public string? DomainPassword { get; set; }

}
}
100 changes: 18 additions & 82 deletions src/Application/Services/SearchDeviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using ProgGym.PrinterMonitor.Application.Interfaces;

namespace ProgGym.PrinterMonitor.Application.Services
{
public class SearchDeviceService
public class SearchDeviceService : ISearchDeviceService
{
private readonly MonitorSettings _monitorSettings;

Expand All @@ -18,87 +20,21 @@
_monitorSettings = monitorSettings;
}

//static void Main(string[] args)
//{
// List<string> printers = new List<string>();
// string subnet = "192.168.1.";
// for (int i = 1; i < 255; i++)
// {
// string ip = subnet + i.ToString();
// Ping ping = new Ping();
// PingReply pingReply = ping.Send(ip, 1000);
// if (pingReply.Status == IPStatus.Success)
// {
// try
// {
// IPHostEntry host = Dns.GetHostEntry(ip);
// if (host != null)
// {
// foreach (IPAddress address in host.AddressList)
// {
// if (address.AddressFamily == AddressFamily.InterNetwork)
// {
// string printerName = GetPrinterName(address.ToString());
// if (!string.IsNullOrEmpty(printerName))
// {
// printers.Add(printerName);
// }
// }
// }
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine(ex.Message);
// }
// }
// }

// if (printers.Count > 0)
// {
// string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Printers;Integrated Security=True";
// using (SqlConnection connection = new SqlConnection(connectionString))
// {
// connection.Open();
// foreach (string printer in printers)
// {
// string query = "INSERT INTO Printers (Name) VALUES (@Name)";
// using (SqlCommand command = new SqlCommand(query, connection))
// {
// command.Parameters.AddWithValue("@Name", printer);
// command.ExecuteNonQuery();
// }
// }
// }
// }
//}
public async Task <List<string>> GetPrinters()

Check warning on line 23 in src/Application/Services/SearchDeviceService.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 23 in src/Application/Services/SearchDeviceService.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
List<string> printers = new List<string>();
DirectoryEntry root = new(_monitorSettings.DomainPath,
_monitorSettings.DomainUserName,
_monitorSettings.DomainPassword);

//static string GetPrinterName(string ipAddress)
//{
// string printerName = null;
// string uri = "http://" + ipAddress + "/dnssd/mfpdiscovery";
// try
// {
// HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
// request.Timeout = 1000;
// request.Method = "GET";
// HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// if (response.StatusCode == HttpStatusCode.OK)
// {
// using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()))
// {
// string responseText = reader.ReadToEnd();
// int start = responseText.IndexOf("name=") + 5;
// int end = responseText.IndexOf(";", start);
// printerName = responseText.Substring(start, end - start);
// }
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine(ex.Message);
// }
// return printerName;
//}
DirectorySearcher searcher = new DirectorySearcher(root);
searcher.Filter = "(objectClass=printQueue)";
searcher.PropertiesToLoad.Add("cn");
foreach (SearchResult result in searcher.FindAll())
{
printers.Add(result.Properties["cn"][0].ToString());

Check warning on line 35 in src/Application/Services/SearchDeviceService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'item' in 'void List<string>.Add(string item)'.

Check warning on line 35 in src/Application/Services/SearchDeviceService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'item' in 'void List<string>.Add(string item)'.
}
return printers;
}
}
}
4 changes: 3 additions & 1 deletion src/PresentationASP/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"Subnet": "",
"DomainPath": "",
"DomainUserName": "",
"DomainPassword": "",
"AllowedHosts": "*"
}
1 change: 1 addition & 0 deletions tests/UnitTest/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
12 changes: 12 additions & 0 deletions tests/UnitTest/SearchDeviceService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProgGym.PrinterMonitor.UnitTest
{
public class SearchDeviceService
{
}
}
20 changes: 20 additions & 0 deletions tests/UnitTest/UnitTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

</Project>