Share via


如何使用選擇性抹除保護檔案 (HTML)

[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]

當您應用程式的使用者不再獲得使用應用程式資料的授權時,您可以使用 [選擇性抹除] 來識別可在應用程式中撤銷的受保護檔案。允許員工在上班時間使用他們自己的裝置,是很常見的商業案例。當員工離開公司時,即會移除他們個人裝置上的公司檔案。

例如,員工攜帶他們個人的平板電腦到辦公室,並使用它來收發商業電子郵件。電子郵件應用程式可以使用 [選擇性抹除],來保護所有用來將公司電子郵件儲存在本機電腦上的檔案。這是透過使用企業識別碼 (例如 "sample.com") 來將這些檔案與公司關聯。如果使用者不再是該公司的員工,當他們下次開啟公司電子郵件應用程式時,該應用程式即可判斷該使用者已不再是員工,並告知 [選擇性抹除] 撤銷針對其企業識別碼所保護之所有檔案的存取權。當應用程式嘗試存取檔案,卻發現其已遭撤銷時,應用程式接著會刪除該檔案。

先決條件

  • 本主題中的程式碼範例假設已設定下列全域變數。

    var appRootFolder = Windows.Storage.ApplicationData.current;
    var enterpriseIdentity = "example.com";
    var accessDeniedHResult = -2147024891;  // Access Denied (0x80070005)
    

使用選擇性抹除保護檔案或資料夾

您可以使用 protectAsync 方法,使用 [選擇性抹除] 來保護檔案或資料夾。這會將檔案識別為已針對您的企業識別碼 (例如 "example.com") 進行保護,如先前程式碼範例所示。如果您使用 protectAsync 方法保護資料夾,則該資料夾中的所有檔案都會繼承相同的保護。

// Add a folder and protect it using Selective Wipe.
function addFolder(folderName) {
    appRootFolder.localFolder.createFolderAsync(folderName).then(
        function (newFolder) {
            protectItem(newFolder);
        });
}

// Add a file and protect it using Selective Wipe.
function addFile(fileName, folder) {
    folder.createFileAsync(fileName).then(
        function (newFile) {
            Windows.Security.EnterpriseData.
                FileRevocationManager.getStatusAsync(newFile).then(
                    function (status) {
                        if (status != Windows.Security.EnterpriseData.
                                FileProtectionStatus.Protected) {
                            protectItem(newFile, enterpriseIdentity);
                        }
                    });
        },
        function (err) {
            // Handle error. For example, file already exists.
        });
}

function protectItem(item, enterpriseIdentity) {
    Windows.Security.EnterpriseData.FileRevocationManager.
        protectAsync(item, enterpriseIdentity).then(
            function (status) {
                return status;
            });
}

撤銷對受保護檔案與資料夾的存取權

當您的應用程式判斷某位使用者已不再有效時,您可以使用 revoke 方法,快速撤銷針對某個企業識別碼所保護之所有檔案和資料夾的存取權,如下列範例所示。revoke 方法不會刪除該檔案。revoke 方法會讓該檔案成為無法存取的狀態。您可以在應用程式中新增程式碼來刪除無法存取且已遭撤銷的檔案,如下一個範例所示。

function initializeApp(userName) {
    if (getUserStatus(userName) == "Not Found") {
        Windows.Security.EnterpriseData.FileRevocationManager.revoke(enterpriseIdentity);
    }
}

取得檔案的狀態

您可以使用 getStatusAsync 方法,來判斷檔案或資料夾的 [選擇性抹除] 保護狀態。這將告知您檔案是否受到保護、檔案是否受到電腦上其他使用者所保護等。getStatusAsync 方法的常見用法是判斷何時應刪除受保護的檔案。例如,在撤銷受保護的檔案後,嘗試存取該檔案內容將導致「拒絕存取」例外狀況。當您遇到該例外狀況時,可以使用 getStatusAsync 方法,來判斷 [選擇性抹除] 是否已撤銷該檔案,若已撤銷,即可刪除該檔案,如下列範例所示。

function getFileContents(filePath) {
    var stream;
    var file;

    Windows.Storage.StorageFile.getFileFromPathAsync(filePath).then(
        function (f) {
            file = f;
            file.openReadAsync().then(
                function (s) {
                    stream = s;
                    return stream;
                },
                function (err) {
                    if (err.number == accessDeniedHResult) {
                        // Delete file if it has been revoked.
                        selectiveWipeCleanup(file);
                    }
                });
        });

    return null;
}

// Delete items revoked by Selective Wipe.
function selectiveWipeCleanup(file) {
    Windows.Security.EnterpriseData.FileRevocationManager.
        getStatusAsync(file).then(
            function (status) {
                if (status == 
                    Windows.Security.EnterpriseData.FileProtectionStatus.revoked) {
                        file.deleteAsync();
                }
        });

}

複製受保護的檔案

當您使用 copyAsynccopyAndReplaceAsync 方法複製檔案時,來自複製檔案的 [選擇性抹除] 保護將不會自動套用到檔案的新複本。這會套用到您「另存」為新檔案的檔案。在此情況下,您可以使用 copyProtectionAsync 方法,從原始檔案複製 [選擇性抹除] 保護,如下列範例所示。

function copyFile(file, newFolder) {
    file.copyAsync(newFolder).then(
        function (newFile) {
            Windows.Security.EnterpriseData.FileRevocationManager.
                copyProtectionAsync(file, newFile);
        },
        function (err) {
            // Handle error. For example, copy already exists.
        });
}

完整範例

相關主題

FileRevocationManager 範例

Windows.Security.EnterpriseData namespace

保護應用程式