Get my application to be allowed access through fi

2019-02-28 08:16发布

问题:

i am trying to get my application to be allowed through firewall, as I have to do ftp in active and passive mode is not an option as servers are not configured for that. so i tried the below code which compiles fine, I exexcute it using:

 MyApp.Classes.INetFwMgr mgr = new MyApp.Classes.INetFwMgr();
    mgr.AuthorizeApplication(Application.ProductName, Application.StartupPath,
        NET_FW_SCOPE_.NET_FW_SCOPE_ALL,
        NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY);

And the class which does the job:

private const string CLSID_FIREWALL_MANAGER = 
"{304CE942-6E39-40D8-943A-B913C40C9CD4}"; 
private static NetFwTypeLib.INetFwMgr GetFirewallManager() 
{ 
Type objectType = Type.GetTypeFromCLSID( 
new Guid(CLSID_FIREWALL_MANAGER)); 
return Activator.CreateInstance(objectType) 
as NetFwTypeLib.INetFwMgr; 
} 


private const string PROGID_AUTHORIZED_APPLICATION = 
"HNetCfg.FwAuthorizedApplication"; 
public bool AuthorizeApplication(string title, string applicationPath, 
NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion) 
{ 
// Create the type from prog id 
Type type = Type.GetTypeFromProgID(PROGID_AUTHORIZED_APPLICATION); 
INetFwAuthorizedApplication auth = Activator.CreateInstance(type) 
as INetFwAuthorizedApplication; 
auth.Name = title; 
auth.ProcessImageFileName = applicationPath; //Getting Access Denied Exception Here
auth.Scope = scope; 
auth.IpVersion = ipVersion; 
auth.Enabled = true; 
NetFwTypeLib.INetFwMgr manager = GetFirewallManager(); 
try 
{ 
manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth); 
} 
catch (Exception ex) 
{ 
return false; 
} 
return true; 
}

using above code, but i get Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) c# exception on line

auth.ProcessImageFileName = applicationPath;

any ideas what to do ?

Edit1: How would i run this as an admin using code?

Edit2: I also tried Putting <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> in manifest did not make a difference

P.S.This programs execution context can be Win 7, vista, xp

回答1:

Firewall management is a system level security feature and has to be done outside of user mode application code. Configuration must be done by an administrator.

It is bad practice to write the code that you wrote and assume that your application will be run as administrator. Even if it is run by an administrator, you now have an application that "does FTP stuff" and "does firewall stuff". No application has ever been written like this.

You can write code that interacts with the system firewall, and that code must be run with elevated permissions. Typically such "helper applications" are never even created however as Windows (and every other OS) has all the necessary management tools shipped with the OS (i.e. wf.msc).



回答2:

i have observed that if i change the order of ftp download statements to following windows dialog appears asking that do you want to allow this program access through firewall; if i click allow access the code works perfectly.

requestDownload = (FtpWebRequest)WebRequest.Create(uri);
                            requestDownload.UsePassive = false;
                            requestDownload.KeepAlive = false;
                            requestDownload.UseBinary = true;
                            requestDownload.Method = WebRequestMethods.Ftp.DownloadFile;


                            requestDownload.Credentials = new NetworkCredential(ftpInfoDownload[3], ftpInfoDownload[4]);

                            responseDownload = (FtpWebResponse)requestDownload.GetResponse();
                            Stream ftpStream = responseDownload.GetResponseStream();


回答3:

Try opening the FTP ports in the firewall -- ports 20 and 21 -- and see if that solves your issue.



回答4:

For running as a different user: Run Code as a different user (C#)

As for getting through the firewall, have you talked to the person/group responsible for the firewall security? They may have some rules in place that you could use.