【问题标题】:is there a way to pre-authorize windows app for internet access?有没有办法预先授权 Windows 应用程序访问互联网?
【发布时间】:2015-10-12 16:30:07
【问题描述】:

对不起,如果有更好的地方可以问这个问题......

我在我们的应用程序中添加了一个 FTP 子系统接口 - 以便我们的脚本可以从我们的 ftp 服务器下载一些文件,以确定是否有新版本可用...

但执行此操作时,Windows 会发出安全提示 - 是否允许此应用程序...

这显然有时会出现在我们的应用窗口下方...

因此,最好在安装时让我们的应用获得授权。但是有没有办法做到这一点?如果不是,那么其他使用互联网的应用程序如何不经常显示此对话框(作为一个明显的例子:Chrome)?

肯定有一种方法可以在安装时标记/注册/授权应用程序,以便在每次尝试检查新版本时绕过此安全警告?

我已经尝试了许多“网络搜索”,但都没有有用的结果,所以我一定是从一个不寻常的方向来解决这个问题的?

【问题讨论】:

  • 假设您谈论的是桌面应用程序,而不是现代应用程序,则该安全提示提供添加 Windows 防火墙例外。如果您在安装过程中正确配置了 Windows 防火墙,则不会出现提示。我自己对 API 并不熟悉,但是 try a search on MSDN.
  • 这听起来很有希望。谢谢。

标签: windows sockets security ftp


【解决方案1】:

如果其他人正在寻找解决方案 - 这是我想出的核心:

CComPtr<INetFwProfile> GetCurrentFirewallProfile()
{
    // we need the manager...
    CComPtr<INetFwMgr> pMgr;
    HRESULT hr = CoCreateInstance(__uuidof(NetFwMgr), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pMgr));
    if (FAILED(hr))
        throw CComException(_T("Unable to create INetFwMgr instance"), hr);

    // ...to get the current policy...
    CComPtr<INetFwPolicy> pPolicy;
    hr = pMgr->get_LocalPolicy(&pPolicy);
    if (FAILED(hr))
        throw CComException(_T("Unable to obtain the INetFwPolicy from the INetFwMgr"), hr);

    // ...to get the current profile
    CComPtr<INetFwProfile> pProfile;
    hr = pPolicy->get_CurrentProfile(&pProfile);
    if (FAILED(hr))
        throw CComException(_T("Unable to obtain the INetFwProfile from the INetFwPolicy"), hr);

    return pProfile;
}

void AuthorizeAppInWindowsFirewall(CComPtr<INetFwProfile> pProfile, const CString executable, const CString name)
{
    // check if this app is already listed
    CComPtr<INetFwAuthorizedApplications> pApps;
    HRESULT hr = pProfile->get_AuthorizedApplications(&pApps);
    if (FAILED(hr))
        throw CComException(_T("Unable to obtain the INetFwAuthorizedApplications from the INetFwProfile"), hr);

    // allocate a com string for our app name
    CComBSTR bstrExecutable(executable);

    CComPtr<INetFwAuthorizedApplication> pApp;
    hr = pApps->Item(bstrExecutable, &pApp);
    if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
    {
        // create one for our app
        hr = CoCreateInstance(__uuidof(NetFwAuthorizedApplication), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pApp));
        if (FAILED(hr))
            throw CComException(_T("Unable to create INetFwAuthorizedApplication instance"), hr);

        // Set the file name
        hr = pApp->put_ProcessImageFileName(bstrExecutable);
        if (FAILED(hr))
            throw CComException(_T("INetFwAuthorizedApplication::put_ProcessImageFileName failed:"), hr);

        // set the application friendly name
        CComBSTR bstrName(name);
        hr = pApp->put_Name(bstrName);
        if (FAILED(hr))
            throw CComException(_T("INetFwAuthorizedApplication::put_Name failed:"), hr);

        // add to list
        hr = pApps->Add(pApp);
        if (FAILED(hr))
            throw CComException(_T("INetFwAuthorizedApplications::Add failed:"), hr);
    }
    else if (FAILED(hr))
        throw CComException(_T("INetFwAuthorizedApplications::Item failed:"), hr);

    // it's already in the list - ensure it is enabled
    VARIANT_BOOL bEnabled;
    hr = pApp->get_Enabled(&bEnabled);
    if (FAILED(hr))
        throw CComException(_T("INetFwAuthorizedApplication::get_Enabled failed:"), hr);

    // not enabled?
    if (bEnabled == VARIANT_FALSE)
    {
        // disabled - let's attempt to enable it!
        hr = pApp->put_Enabled(VARIANT_TRUE);
        if (FAILED(hr))
            throw CComException(_T("INetFwAuthorizedApplication::put_Enabled failed:"), hr);
    }
}

【讨论】:

    猜你喜欢
    • 2016-06-03
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2023-01-05
    • 2020-09-26
    • 2018-08-28
    • 1970-01-01
    相关资源
    最近更新 更多