SecurityContext.Run(SecurityContext, ContextCallback, Object) Method

Definition

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

public:
 static void Run(System::Security::SecurityContext ^ securityContext, System::Threading::ContextCallback ^ callback, System::Object ^ state);
public static void Run (System.Security.SecurityContext securityContext, System.Threading.ContextCallback callback, object state);
[System.Security.SecurityCritical]
public static void Run (System.Security.SecurityContext securityContext, System.Threading.ContextCallback callback, object state);
static member Run : System.Security.SecurityContext * System.Threading.ContextCallback * obj -> unit
[<System.Security.SecurityCritical>]
static member Run : System.Security.SecurityContext * System.Threading.ContextCallback * obj -> unit
Public Shared Sub Run (securityContext As SecurityContext, callback As ContextCallback, state As Object)

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.

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);
    }
}
Imports System.Security
Imports System.Threading



Class Test
    
    Shared Sub Main() 
        Dim cCallBack As New ContextCallback(AddressOf Callback)
        SecurityContext.Run(SecurityContext.Capture(), cCallBack, "Hello world.")
    End Sub
    
    Shared Sub Callback(ByVal o As Object) 
        Console.WriteLine(o)
    
    End Sub
End Class

Remarks

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

Applies to