SecurityContext.Run(SecurityContext, ContextCallback, Object) SecurityContext.Run(SecurityContext, ContextCallback, Object) 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);
[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

Parameters

securityContext
SecurityContext SecurityContext SecurityContext

The security context to set.

callback
ContextCallback ContextCallback ContextCallback

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

state
Object Object Object

The object to pass to the callback method.

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.

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
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 'Main
    
    Shared Sub Callback(ByVal o As Object) 
        Console.WriteLine(o)
    
    End Sub 'Callback
End Class 'Test

Remarks

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

Security

SecurityCriticalAttribute
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Applies to