UnauthorizedAccessException when creating a mutex
Given the following piece of code that uses a 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);
}
For some of my users, when the Mutex gets created (in the catch block), an UnauthorizedAccessException is thrown. I'm trying to find what cause this exception.
The message is
Access to the path 'Global\MyMutex' is denied.
I know that some of the users having the problem are not administrator of their pc. I've read on MSDN about Mutex and MutexAccessRule, but it is not clear to me where I can assign or see permissions related to Mutex for a given user.
I suspect it is not the Mutex rights, but the right to create objects in Global\ namespace (SeCreateGlobalPrivilege).
Also, if this code can be called by multiple processes at about the same time - you can have a race condition here.
精彩评论