Mutex.SetAccessControl(MutexSecurity) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
명명된 시스템 뮤텍스에 액세스 제어 보안을 설정합니다.
public:
void SetAccessControl(System::Security::AccessControl::MutexSecurity ^ mutexSecurity);
public void SetAccessControl (System.Security.AccessControl.MutexSecurity mutexSecurity);
member this.SetAccessControl : System.Security.AccessControl.MutexSecurity -> unit
Public Sub SetAccessControl (mutexSecurity As MutexSecurity)
매개 변수
- mutexSecurity
- MutexSecurity
명명된 시스템 뮤텍스에 적용할 액세스 제어 보안을 나타내는 MutexSecurity 개체입니다.
예외
mutexSecurity
이(가) null
인 경우
사용자에게 ChangePermissions가 없는 경우
또는 뮤텍스가 ChangePermissions를 사용하여 열리지 않은 경우
현재 Mutex 개체가 명명된 시스템 뮤텍스를 나타내지 않는 경우
예제
다음 코드 예제에서는 액세스 제어 보안을 사용하여 명명된 뮤텍스의 교차 프로세스 동작을 보여 줍니다. 이 예제에서는 메서드 오버로드를 사용하여 OpenExisting(String) 명명된 뮤텍스가 있는지 테스트합니다.
뮤텍스가 없는 경우 현재 사용자에게 뮤텍스를 사용할 수 있는 권한을 거부하지만 뮤텍스에 대한 읽기 및 변경 권한을 부여하는 초기 소유권 및 액세스 제어 보안으로 만들어집니다.
두 명령 창에서 컴파일된 예제를 실행하는 경우 두 번째 복사본은 호출에서 액세스 위반 예외를 throw합니다 OpenExisting(String). 예외가 catch되고, 이 예제에서는 메서드 오버로드를 사용하여 OpenExisting(String, MutexRights) 권한 및 메서드를 사용하여 사용 권한을 읽고 변경하는 데 필요한 권한으로 뮤텍스를 GetAccessControl SetAccessControl 엽니다.
사용 권한이 변경되면 뮤텍스를 입력하고 해제하는 데 필요한 권한으로 뮤텍스가 열립니다. 세 번째 명령 창에서 컴파일된 예제를 실행하면 새 권한을 사용하여 실행됩니다.
using namespace System;
using namespace System::Threading;
using namespace System::Security::AccessControl;
using namespace System::Security::Permissions;
public ref class Example
{
public:
[SecurityPermissionAttribute(SecurityAction::Demand,Flags=SecurityPermissionFlag::UnmanagedCode)]
static void Main()
{
String^ mutexName = L"MutexExample4";
Mutex^ m = nullptr;
bool doesNotExist = false;
bool unauthorized = false;
// The value of this variable is set by the mutex
// constructor. It is true if the named system mutex was
// created, and false if the named mutex already existed.
//
bool mutexWasCreated = false;
// Attempt to open the named mutex.
try
{
// Open the mutex with (MutexRights.Synchronize |
// MutexRights.Modify), to enter and release the
// named mutex.
//
m = Mutex::OpenExisting( mutexName );
}
catch ( WaitHandleCannotBeOpenedException^ )
{
Console::WriteLine( L"Mutex does not exist." );
doesNotExist = true;
}
catch ( UnauthorizedAccessException^ ex )
{
Console::WriteLine( L"Unauthorized access: {0}", ex->Message );
unauthorized = true;
}
// There are three cases: (1) The mutex does not exist.
// (2) The mutex exists, but the current user doesn't
// have access. (3) The mutex exists and the user has
// access.
//
if ( doesNotExist )
{
// The mutex does not exist, so create it.
// Create an access control list (ACL) that denies the
// current user the right to enter or release the
// mutex, but allows the right to read and change
// security information for the mutex.
//
String^ user = String::Concat( Environment::UserDomainName, L"\\",
Environment::UserName );
MutexSecurity^ mSec = gcnew MutexSecurity;
MutexAccessRule^ rule = gcnew MutexAccessRule( user,
static_cast<MutexRights>(
MutexRights::Synchronize |
MutexRights::Modify),
AccessControlType::Deny );
mSec->AddAccessRule( rule );
rule = gcnew MutexAccessRule( user,
static_cast<MutexRights>(
MutexRights::ReadPermissions |
MutexRights::ChangePermissions),
AccessControlType::Allow );
mSec->AddAccessRule( rule );
// Create a Mutex object that represents the system
// mutex named by the constant 'mutexName', with
// initial ownership for this thread, and with the
// specified security access. The Boolean value that
// indicates creation of the underlying system object
// is placed in mutexWasCreated.
//
m = gcnew Mutex( true,mutexName, mutexWasCreated,mSec );
// If the named system mutex was created, it can be
// used by the current instance of this program, even
// though the current user is denied access. The current
// program owns the mutex. Otherwise, exit the program.
//
if ( mutexWasCreated )
{
Console::WriteLine( L"Created the mutex." );
}
else
{
Console::WriteLine( L"Unable to create the mutex." );
return;
}
}
else if ( unauthorized )
{
// Open the mutex to read and change the access control
// security. The access control security defined above
// allows the current user to do this.
//
try
{
m = Mutex::OpenExisting( mutexName,
static_cast<MutexRights>(
MutexRights::ReadPermissions |
MutexRights::ChangePermissions) );
// Get the current ACL. This requires
// MutexRights.ReadPermissions.
MutexSecurity^ mSec = m->GetAccessControl();
String^ user = String::Concat( Environment::UserDomainName,
L"\\", Environment::UserName );
// First, the rule that denied the current user
// the right to enter and release the mutex must
// be removed.
MutexAccessRule^ rule = gcnew MutexAccessRule( user,
static_cast<MutexRights>(
MutexRights::Synchronize |
MutexRights::Modify),
AccessControlType::Deny );
mSec->RemoveAccessRule( rule );
// Now grant the user the correct rights.
//
rule = gcnew MutexAccessRule( user,
static_cast<MutexRights>(
MutexRights::Synchronize |
MutexRights::Modify),
AccessControlType::Allow );
mSec->AddAccessRule( rule );
// Update the ACL. This requires
// MutexRights.ChangePermissions.
m->SetAccessControl( mSec );
Console::WriteLine( L"Updated mutex security." );
// Open the mutex with (MutexRights.Synchronize
// | MutexRights.Modify), the rights required to
// enter and release the mutex.
//
m = Mutex::OpenExisting( mutexName );
}
catch ( UnauthorizedAccessException^ ex )
{
Console::WriteLine(
L"Unable to change permissions: {0}", ex->Message );
return;
}
}
// If this program created the mutex, it already owns
// the mutex.
//
if ( !mutexWasCreated )
{
// Enter the mutex, and hold it until the program
// exits.
//
try
{
Console::WriteLine( L"Wait for the mutex." );
m->WaitOne();
Console::WriteLine( L"Entered the mutex." );
}
catch ( UnauthorizedAccessException^ ex )
{
Console::WriteLine( L"Unauthorized access: {0}",
ex->Message );
}
}
Console::WriteLine( L"Press the Enter key to exit." );
Console::ReadLine();
m->ReleaseMutex();
m->Dispose();
}
};
int main()
{
Example::Main();
}
using System;
using System.Threading;
using System.Security.AccessControl;
internal class Example
{
internal static void Main()
{
const string mutexName = "MutexExample4";
Mutex m = null;
bool doesNotExist = false;
bool unauthorized = false;
// The value of this variable is set by the mutex
// constructor. It is true if the named system mutex was
// created, and false if the named mutex already existed.
//
bool mutexWasCreated = false;
// Attempt to open the named mutex.
try
{
// Open the mutex with (MutexRights.Synchronize |
// MutexRights.Modify), to enter and release the
// named mutex.
//
m = Mutex.OpenExisting(mutexName);
}
catch(WaitHandleCannotBeOpenedException)
{
Console.WriteLine("Mutex does not exist.");
doesNotExist = true;
}
catch(UnauthorizedAccessException ex)
{
Console.WriteLine("Unauthorized access: {0}", ex.Message);
unauthorized = true;
}
// There are three cases: (1) The mutex does not exist.
// (2) The mutex exists, but the current user doesn't
// have access. (3) The mutex exists and the user has
// access.
//
if (doesNotExist)
{
// The mutex does not exist, so create it.
// Create an access control list (ACL) that denies the
// current user the right to enter or release the
// mutex, but allows the right to read and change
// security information for the mutex.
//
string user = Environment.UserDomainName + "\\"
+ Environment.UserName;
var mSec = new MutexSecurity();
MutexAccessRule rule = new MutexAccessRule(user,
MutexRights.Synchronize | MutexRights.Modify,
AccessControlType.Deny);
mSec.AddAccessRule(rule);
rule = new MutexAccessRule(user,
MutexRights.ReadPermissions | MutexRights.ChangePermissions,
AccessControlType.Allow);
mSec.AddAccessRule(rule);
// Create a Mutex object that represents the system
// mutex named by the constant 'mutexName', with
// initial ownership for this thread, and with the
// specified security access. The Boolean value that
// indicates creation of the underlying system object
// is placed in mutexWasCreated.
//
m = new Mutex(true, mutexName, out mutexWasCreated, mSec);
// If the named system mutex was created, it can be
// used by the current instance of this program, even
// though the current user is denied access. The current
// program owns the mutex. Otherwise, exit the program.
//
if (mutexWasCreated)
{
Console.WriteLine("Created the mutex.");
}
else
{
Console.WriteLine("Unable to create the mutex.");
return;
}
}
else if (unauthorized)
{
// Open the mutex to read and change the access control
// security. The access control security defined above
// allows the current user to do this.
//
try
{
m = Mutex.OpenExisting(mutexName,
MutexRights.ReadPermissions | MutexRights.ChangePermissions);
// Get the current ACL. This requires
// MutexRights.ReadPermissions.
MutexSecurity mSec = m.GetAccessControl();
string user = Environment.UserDomainName + "\\"
+ Environment.UserName;
// First, the rule that denied the current user
// the right to enter and release the mutex must
// be removed.
MutexAccessRule rule = new MutexAccessRule(user,
MutexRights.Synchronize | MutexRights.Modify,
AccessControlType.Deny);
mSec.RemoveAccessRule(rule);
// Now grant the user the correct rights.
//
rule = new MutexAccessRule(user,
MutexRights.Synchronize | MutexRights.Modify,
AccessControlType.Allow);
mSec.AddAccessRule(rule);
// Update the ACL. This requires
// MutexRights.ChangePermissions.
m.SetAccessControl(mSec);
Console.WriteLine("Updated mutex security.");
// Open the mutex with (MutexRights.Synchronize
// | MutexRights.Modify), the rights required to
// enter and release the mutex.
//
m = Mutex.OpenExisting(mutexName);
}
catch(UnauthorizedAccessException ex)
{
Console.WriteLine("Unable to change permissions: {0}",
ex.Message);
return;
}
}
// If this program created the mutex, it already owns
// the mutex.
//
if (!mutexWasCreated)
{
// Enter the mutex, and hold it until the program
// exits.
//
try
{
Console.WriteLine("Wait for the mutex.");
m.WaitOne();
Console.WriteLine("Entered the mutex.");
}
catch(UnauthorizedAccessException ex)
{
Console.WriteLine("Unauthorized access: {0}", ex.Message);
}
}
Console.WriteLine("Press the Enter key to exit.");
Console.ReadLine();
m.ReleaseMutex();
m.Dispose();
}
}
Imports System.Threading
Imports System.Security.AccessControl
Friend Class Example
<MTAThread> _
Friend Shared Sub Main()
Const mutexName As String = "MutexExample4"
Dim m As Mutex = Nothing
Dim doesNotExist as Boolean = False
Dim unauthorized As Boolean = False
' The value of this variable is set by the mutex
' constructor. It is True if the named system mutex was
' created, and False if the named mutex already existed.
'
Dim mutexWasCreated As Boolean
' Attempt to open the named mutex.
Try
' Open the mutex with (MutexRights.Synchronize Or
' MutexRights.Modify), to enter and release the
' named mutex.
'
m = Mutex.OpenExisting(mutexName)
Catch ex As WaitHandleCannotBeOpenedException
Console.WriteLine("Mutex does not exist.")
doesNotExist = True
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unauthorized access: {0}", ex.Message)
unauthorized = True
End Try
' There are three cases: (1) The mutex does not exist.
' (2) The mutex exists, but the current user doesn't
' have access. (3) The mutex exists and the user has
' access.
'
If doesNotExist Then
' The mutex does not exist, so create it.
' Create an access control list (ACL) that denies the
' current user the right to enter or release the
' mutex, but allows the right to read and change
' security information for the mutex.
'
Dim user As String = Environment.UserDomainName _
& "\" & Environment.UserName
Dim mSec As New MutexSecurity()
Dim rule As New MutexAccessRule(user, _
MutexRights.Synchronize Or MutexRights.Modify, _
AccessControlType.Deny)
mSec.AddAccessRule(rule)
rule = New MutexAccessRule(user, _
MutexRights.ReadPermissions Or _
MutexRights.ChangePermissions, _
AccessControlType.Allow)
mSec.AddAccessRule(rule)
' Create a Mutex object that represents the system
' mutex named by the constant 'mutexName', with
' initial ownership for this thread, and with the
' specified security access. The Boolean value that
' indicates creation of the underlying system object
' is placed in mutexWasCreated.
'
m = New Mutex(True, mutexName, mutexWasCreated, mSec)
' If the named system mutex was created, it can be
' used by the current instance of this program, even
' though the current user is denied access. The current
' program owns the mutex. Otherwise, exit the program.
'
If mutexWasCreated Then
Console.WriteLine("Created the mutex.")
Else
Console.WriteLine("Unable to create the mutex.")
Return
End If
ElseIf unauthorized Then
' Open the mutex to read and change the access control
' security. The access control security defined above
' allows the current user to do this.
'
Try
m = Mutex.OpenExisting(mutexName, _
MutexRights.ReadPermissions Or _
MutexRights.ChangePermissions)
' Get the current ACL. This requires
' MutexRights.ReadPermissions.
Dim mSec As MutexSecurity = m.GetAccessControl()
Dim user As String = Environment.UserDomainName _
& "\" & Environment.UserName
' First, the rule that denied the current user
' the right to enter and release the mutex must
' be removed.
Dim rule As New MutexAccessRule(user, _
MutexRights.Synchronize Or MutexRights.Modify, _
AccessControlType.Deny)
mSec.RemoveAccessRule(rule)
' Now grant the user the correct rights.
'
rule = New MutexAccessRule(user, _
MutexRights.Synchronize Or MutexRights.Modify, _
AccessControlType.Allow)
mSec.AddAccessRule(rule)
' Update the ACL. This requires
' MutexRights.ChangePermissions.
m.SetAccessControl(mSec)
Console.WriteLine("Updated mutex security.")
' Open the mutex with (MutexRights.Synchronize
' Or MutexRights.Modify), the rights required to
' enter and release the mutex.
'
m = Mutex.OpenExisting(mutexName)
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unable to change permissions: {0}", _
ex.Message)
Return
End Try
End If
' If this program created the mutex, it already owns
' the mutex.
'
If Not mutexWasCreated Then
' Enter the mutex, and hold it until the program
' exits.
'
Try
Console.WriteLine("Wait for the mutex.")
m.WaitOne()
Console.WriteLine("Entered the mutex.")
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unauthorized access: {0}", _
ex.Message)
End Try
End If
Console.WriteLine("Press the Enter key to exit.")
Console.ReadLine()
m.ReleaseMutex()
m.Dispose()
End Sub
End Class
설명
사용자에게 이 메서드를 호출할 수 있는 권한이 있어야 MutexRights.ChangePermissions 하며 뮤텍스가 .로 열려 MutexRights.ChangePermissions있어야 합니다.
참고
.NET Core 및 .NET 5 이상에서 해당 메서드는 SetAccessControl.