From b31d54bd8e228dc77d43502e99086402b0a8641b Mon Sep 17 00:00:00 2001 From: misya Date: Wed, 30 Apr 2025 11:51:23 +0800 Subject: [PATCH] added network access service --- Areas/MMS/Models/NetworkAccessService.cs | 80 ++++++++++++++++++++++++ Program.cs | 2 + appsettings.json | 8 +++ 3 files changed, 90 insertions(+) create mode 100644 Areas/MMS/Models/NetworkAccessService.cs diff --git a/Areas/MMS/Models/NetworkAccessService.cs b/Areas/MMS/Models/NetworkAccessService.cs new file mode 100644 index 0000000..9880e27 --- /dev/null +++ b/Areas/MMS/Models/NetworkAccessService.cs @@ -0,0 +1,80 @@ +using System.Runtime.InteropServices; +using System.Security; + +public class NetworkAccessService +{ + private readonly IConfiguration _config; + + public NetworkAccessService(IConfiguration config) + { + _config = config; + } + + public void ConnectToNetworkShare(string networkPath) + { + var username = _config["NetworkCredentials:ImageServer:Username"]; + var password = _config["NetworkCredentials:ImageServer:Password"]; + var domain = _config["NetworkCredentials:ImageServer:Domain"] ?? "."; + + var netResource = new NetResource + { + Scope = ResourceScope.GlobalNetwork, + ResourceType = ResourceType.Disk, + RemoteName = networkPath + }; + + var result = WNetAddConnection2(netResource, password, username, 0); + if (result != 0) + { + throw new Exception($"Failed to connect to {networkPath}. Error code: {result}"); + } + } + + public void DisconnectFromNetworkShare(string networkPath) + { + WNetCancelConnection2(networkPath, 0, true); + } + + [StructLayout(LayoutKind.Sequential)] + private class NetResource + { + public ResourceScope Scope; + public ResourceType ResourceType; + public ResourceDisplayType DisplayType; + public int Usage; + public string LocalName; + public string RemoteName; + public string Comment; + public string Provider; + } + + private enum ResourceScope : int + { + Connected = 1, + GlobalNetwork = 2, + Remembered = 3, + } + + private enum ResourceType : int + { + Any = 0, + Disk = 1, + Print = 2, + } + + private enum ResourceDisplayType : int + { + Generic = 0x0, + Domain = 0x01, + Server = 0x02, + Share = 0x03, + File = 0x04, + Group = 0x05, + } + + [DllImport("mpr.dll")] + private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags); + + [DllImport("mpr.dll")] + private static extern int WNetCancelConnection2(string name, int flags, bool force); +} diff --git a/Program.cs b/Program.cs index 546d518..88bafcf 100644 --- a/Program.cs +++ b/Program.cs @@ -7,6 +7,7 @@ using PSTW_CentralSystem.Models; using Serilog; using QuestPDF; using QuestPDF.Infrastructure; +using System.Net; internal class Program { @@ -21,6 +22,7 @@ internal class Program // Add services to the container. builder.Services.AddControllersWithViews(); builder.Services.AddRazorPages(); + builder.Services.AddScoped(); Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(builder.Configuration) diff --git a/appsettings.json b/appsettings.json index 81b3254..0254788 100644 --- a/appsettings.json +++ b/appsettings.json @@ -8,6 +8,14 @@ //"DefaultConnection": "Server=219.92.7.60;Port=3307;uid=intern;password='intern_mysql_acct';database=web_interface;"//DB_dev connection "MMSDatabase": "Server=192.168.12.42;Port=3306;Uid=mmsuser;password=mms@pstw_mysql_root;database=db_mms;ConvertZeroDateTime=True;" }, + "NetworkCredentials": { + "ImageServer": { + "Username": "installer", + "Password": "mms@pstw", + "Domain": "." + } + }, + "PhotoBasePath": "\\192.168.12.42\\mms\\marine\\manual_tarball", "Logging": { "LogLevel": { "Default": "Information",