added network access service
This commit is contained in:
parent
0556e4763e
commit
b31d54bd8e
80
Areas/MMS/Models/NetworkAccessService.cs
Normal file
80
Areas/MMS/Models/NetworkAccessService.cs
Normal file
@ -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);
|
||||
}
|
||||
@ -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<NetworkAccessService>();
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(builder.Configuration)
|
||||
|
||||
@ -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",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user