| 
1 |  | -using Files.App.Shell;  | 
 | 1 | +using Files.App.Extensions;  | 
 | 2 | +using Files.App.Helpers;  | 
 | 3 | +using Files.App.Shell;  | 
2 | 4 | using System;  | 
3 | 5 | using System.ComponentModel;  | 
4 | 6 | using System.Runtime.InteropServices;  | 
 | 7 | +using System.Text;  | 
5 | 8 | using System.Threading.Tasks;  | 
6 | 9 | using System.Windows.Forms;  | 
7 | 10 | using Vanara.Extensions;  | 
8 | 11 | using Vanara.InteropServices;  | 
 | 12 | +using static Vanara.PInvoke.AdvApi32;  | 
 | 13 | +using Vanara.PInvoke;  | 
9 | 14 | using static Vanara.PInvoke.Mpr;  | 
10 | 15 | 
 
  | 
11 | 16 | namespace Files.App.Filesystem  | 
@@ -117,6 +122,52 @@ public static Task<bool> OpenMapNetworkDriveDialog(long hwnd)  | 
117 | 122 | 			});  | 
118 | 123 | 		}  | 
119 | 124 | 
 
  | 
 | 125 | +		public static async Task<bool> AuthenticateNetworkShare(string path)  | 
 | 126 | +		{  | 
 | 127 | +			NETRESOURCE nr = new NETRESOURCE  | 
 | 128 | +			{  | 
 | 129 | +				dwType = NETRESOURCEType.RESOURCETYPE_DISK,  | 
 | 130 | +				lpRemoteName = path  | 
 | 131 | +			};  | 
 | 132 | + | 
 | 133 | +			Win32Error connectionError = WNetAddConnection3(HWND.NULL, nr, null, null, 0);  // if creds are saved, this will return NO_ERROR  | 
 | 134 | + | 
 | 135 | +			if (connectionError == Win32Error.ERROR_LOGON_FAILURE)  | 
 | 136 | +			{  | 
 | 137 | +				var dialog = DynamicDialogFactory.GetFor_CredentialEntryDialog(path);  | 
 | 138 | +				await dialog.ShowAsync();  | 
 | 139 | +				var credentialsReturned = dialog.ViewModel.AdditionalData as string[];  | 
 | 140 | + | 
 | 141 | +				if (credentialsReturned is string[] && credentialsReturned[1] != null)  | 
 | 142 | +				{  | 
 | 143 | +					connectionError = WNetAddConnection3(HWND.NULL, nr, credentialsReturned[1], credentialsReturned[0], 0);  | 
 | 144 | +					if (credentialsReturned[2] == "y" && connectionError == Win32Error.NO_ERROR)  | 
 | 145 | +					{  | 
 | 146 | +						CREDENTIAL creds = new CREDENTIAL();  | 
 | 147 | +						creds.TargetName = new StrPtrAuto(path.Substring(2));  | 
 | 148 | +						creds.UserName = new StrPtrAuto(credentialsReturned[0]);  | 
 | 149 | +						creds.Type = CRED_TYPE.CRED_TYPE_DOMAIN_PASSWORD;  | 
 | 150 | +						creds.AttributeCount = 0;  | 
 | 151 | +						creds.Persist = CRED_PERSIST.CRED_PERSIST_ENTERPRISE;  | 
 | 152 | +						byte[] bpassword = Encoding.Unicode.GetBytes(credentialsReturned[1]);  | 
 | 153 | +						creds.CredentialBlobSize = (UInt32)bpassword.Length;  | 
 | 154 | +						creds.CredentialBlob = Marshal.StringToCoTaskMemUni(credentialsReturned[1]);  | 
 | 155 | +						CredWrite(creds, 0);  | 
 | 156 | +					}  | 
 | 157 | +				}  | 
 | 158 | +				else  | 
 | 159 | +					return false;  | 
 | 160 | +			}  | 
 | 161 | + | 
 | 162 | +			if (connectionError == Win32Error.NO_ERROR)  | 
 | 163 | +				return true;  | 
 | 164 | +			else  | 
 | 165 | +			{  | 
 | 166 | +				await DialogDisplayHelper.ShowDialogAsync("NetworkFolderErrorDialogTitle".GetLocalizedResource(), connectionError.ToString().Split(":")[1].Trim());  | 
 | 167 | +				return false;  | 
 | 168 | +			}  | 
 | 169 | +		}  | 
 | 170 | + | 
120 | 171 | 		public static bool DisconnectNetworkDrive(string drive)  | 
121 | 172 | 		{  | 
122 | 173 | 			return WNetCancelConnection2(drive.TrimEnd('\\'), CONNECT.CONNECT_UPDATE_PROFILE, true).Succeeded;  | 
 | 
0 commit comments