第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

檢查.NET中的目錄和文件寫入權(quán)限

檢查.NET中的目錄和文件寫入權(quán)限

陪伴而非守候 2019-07-24 19:19:33
檢查.NET中的目錄和文件寫入權(quán)限在我的.NET 2.0應(yīng)用程序中,我需要檢查是否有足夠的權(quán)限來(lái)創(chuàng)建和寫入目錄的文件。為此,我有以下函數(shù)嘗試創(chuàng)建一個(gè)文件并向其寫入一個(gè)字節(jié),然后刪除自己以測(cè)試權(quán)限是否存在。我認(rèn)為檢查的最佳方法是實(shí)際嘗試并執(zhí)行此操作,捕獲發(fā)生的任何異常。雖然我對(duì)一般的異常捕獲并不是特別高興,所以有更好的或者更可接受的方式嗎?private const string TEMP_FILE = "\\tempFile.tmp";/// <summary>/// Checks the ability to create and write to a file in the supplied directory./// </summary>/// <param name="directory">String representing the directory path to check.</param>/// <returns>True if successful; otherwise false.</returns>private static bool CheckDirectoryAccess(string directory){     bool success = false;     string fullPath = directory + TEMP_FILE;     if (Directory.Exists(directory))     {         try         {             using (FileStream fs = new FileStream(fullPath, FileMode.CreateNew,                                                              FileAccess.Write))             {                 fs.WriteByte(0xff);             }             if (File.Exists(fullPath))             {                 File.Delete(fullPath);                 success = true;             }         }         catch (Exception)         {             success = false;         }     }
查看完整描述

3 回答

?
米脂

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊

您應(yīng)該做的是計(jì)算運(yùn)行代碼的用戶標(biāo)識(shí)的有效權(quán)限。例如,以上示例中沒(méi)有一個(gè)正確地考慮了組成員身份。

我很確定Keith Brown在他的Windows安全Windows開(kāi)發(fā)人員指南的維基版本(此時(shí)是離線)中有一些代碼可以執(zhí)行此操作。在他的Programming Windows Security書(shū)籍中也對(duì)此進(jìn)行了詳細(xì)討論。

計(jì)算有效權(quán)限不適合膽小的人,而您的代碼嘗試創(chuàng)建文件并捕獲拋出的安全異??赡苁亲枇ψ钚〉穆窂?。


查看完整回答
反對(duì) 回復(fù) 2019-07-24
?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

Deny優(yōu)先于Allow。本地規(guī)則優(yōu)先于繼承的規(guī)則。我已經(jīng)看到了很多解決方案(包括這里顯示的一些答案),但它們都沒(méi)有考慮規(guī)則是否是繼承的。因此,我建議采用以下方法來(lái)考慮規(guī)則繼承(整齊地包裝到類中):

public class CurrentUserSecurity{
    WindowsIdentity _currentUser;
    WindowsPrincipal _currentPrincipal;

    public CurrentUserSecurity()
    {
        _currentUser = WindowsIdentity.GetCurrent();
        _currentPrincipal = new WindowsPrincipal(_currentUser);
    }

    public bool HasAccess(DirectoryInfo directory, FileSystemRights right)
    {
        // Get the collection of authorization rules that apply to the directory.
        AuthorizationRuleCollection acl = directory.GetAccessControl()
            .GetAccessRules(true, true, typeof(SecurityIdentifier));
        return HasFileOrDirectoryAccess(right, acl);
    }

    public bool HasAccess(FileInfo file, FileSystemRights right)
    {
        // Get the collection of authorization rules that apply to the file.
        AuthorizationRuleCollection acl = file.GetAccessControl()
            .GetAccessRules(true, true, typeof(SecurityIdentifier));
        return HasFileOrDirectoryAccess(right, acl);
    }

    private bool HasFileOrDirectoryAccess(FileSystemRights right,
                                          AuthorizationRuleCollection acl)
    {
        bool allow = false;
        bool inheritedAllow = false;
        bool inheritedDeny = false;

        for (int i = 0; i < acl.Count; i++) {
            var currentRule = (FileSystemAccessRule)acl[i];
            // If the current rule applies to the current user.
            if (_currentUser.User.Equals(currentRule.IdentityReference) ||
                _currentPrincipal.IsInRole(
                                (SecurityIdentifier)currentRule.IdentityReference)) {

                if (currentRule.AccessControlType.Equals(AccessControlType.Deny)) {
                    if ((currentRule.FileSystemRights & right) == right) {
                        if (currentRule.IsInherited) {
                            inheritedDeny = true;
                        } else { // Non inherited "deny" takes overall precedence.
                            return false;
                        }
                    }
                } else if (currentRule.AccessControlType
                                                  .Equals(AccessControlType.Allow)) {
                    if ((currentRule.FileSystemRights & right) == right) {
                        if (currentRule.IsInherited) {
                            inheritedAllow = true;
                        } else {
                            allow = true;
                        }
                    }
                }
            }
        }

        if (allow) { // Non inherited "allow" takes precedence over inherited rules.
            return true;
        }
        return inheritedAllow && !inheritedDeny;
    }}

但是,我的經(jīng)驗(yàn)是,這并不總是適用于遠(yuǎn)程計(jì)算機(jī),因?yàn)槟鸁o(wú)法始終查詢那里的文件訪問(wèn)權(quán)限。在這種情況下的解決方案是嘗試; 甚至可能只是嘗試創(chuàng)建一個(gè)臨時(shí)文件,如果您需要在使用“真實(shí)”文件之前知道訪問(wèn)權(quán)限。


查看完整回答
反對(duì) 回復(fù) 2019-07-24
  • 3 回答
  • 0 關(guān)注
  • 563 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)