创建互斥锁时出现UnauthorizedAccessException异常

3
给定以下使用Mutex的代码片段:
string mutexName = @"Global\MyMutex";

try
{
    _mutex = Mutex.OpenExisting(mutexName, MutexRights.Synchronize);
}
catch (WaitHandleCannotBeOpenedException)
{
    SecurityIdentifier securityIdentifier = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
    MutexSecurity mutexSecurity = new MutexSecurity();
    mutexSecurity.AddAccessRule(new MutexAccessRule(securityIdentifier, MutexRights.Synchronize, AccessControlType.Allow));

    bool dummy = false;

    // Creates the internal Mutex without taking ownership on it.
    _mutex = new Mutex(false, mutexName, out dummy, mutexSecurity);
}

对于我的一些用户,在创建Mutex(在catch块中)时,会抛出UnauthorizedAccessException异常。我正在尝试找到引起此异常的原因。
消息是:
访问路径“Global\MyMutex”被拒绝。
我知道一些遇到问题的用户不是他们电脑的管理员。我已经阅读了MSDN关于Mutex和MutexAccessRule的内容,但我不清楚在哪里可以为给定用户分配或查看与Mutex相关的权限。
1个回答

1

我怀疑问题不在于互斥权,而是在于创建 Global\ 命名空间对象的权限(SeCreateGlobalPrivilege)。

此外,如果该代码可能被多个进程同时调用,则可能存在竞态条件。


对于SeCreateGlobalPrivilege,它非常有趣。 在阅读了关于SeCreateGlobalPrivilege的内容之后,我是否错了或者我不能在我的应用程序中运行时设置此权限?另外一件事,这个权限似乎只适用于终端服务会话中不是管理员、本地系统帐户或服务帐户的用户? - Sebastien
起初,我认为你在谈论线程竞争,但实际上,你是正确的。这段代码用于进程间通信。所以我需要解决进程竞争问题。我该如何保护代码免受此影响? - Sebastien

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接