SecurityContext.Run(SecurityContext, ContextCallback, Object) Method

Definition

Runs the specified method in the specified security context on the current thread.

C#
public static void Run(System.Security.SecurityContext securityContext, System.Threading.ContextCallback callback, object state);
C#
[System.Security.SecurityCritical]
public static void Run(System.Security.SecurityContext securityContext, System.Threading.ContextCallback callback, object state);

Parameters

securityContext
SecurityContext

The security context to set.

callback
ContextCallback

The delegate that represents the method to run in the specified security context.

state
Object

The object to pass to the callback method.

Attributes

Exceptions

securityContext is null.

-or-

securityContext was not acquired through a capture operation.

-or-

securityContext has already been used as the argument to a Run(SecurityContext, ContextCallback, Object) method call.

.NET 5+ (including .NET Core): In all cases.

Examples

The following code example shows how to use the Run method to execute a method in a specified security context.

C#
using System;
using System.Security;
using System.Threading;

class Test
{
    static void Main()
    {
        SecurityContext.Run(SecurityContext.Capture(), 
            new ContextCallback(Callback), "Hello world.");
    }
    static void Callback(object o)
    {
        Console.WriteLine(o);
    }
}

Remarks

The security context for the current thread is returned to its previous state when the method call is complete.

Applies to

Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9