Thread..::.CurrentPrincipal Property Home
This page is specific to:.NET Framework Version:1.12.03.03.54.0
.NET Framework Class Library
Thread..::.CurrentPrincipal Property

Gets or sets the thread's current principal (for role-based security).

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)
Syntax

'Usage

Dim value As IPrincipal

value = Thread.CurrentPrincipal

Thread.CurrentPrincipal = value

'Declaration

Public Shared Property CurrentPrincipal As IPrincipal

Property Value

Type: System.Security.Principal..::.IPrincipal
An IPrincipal value representing the security context.
Exceptions

ExceptionCondition
SecurityException

The caller does not have the permission required to set the principal.

Examples

The following code example shows how to set and retrieve the principal of a thread.

Option Explicit
Option Strict

Imports Microsoft.VisualBasic
Imports System
Imports System.Security
Imports System.Security.Permissions
Imports System.Security.Principal
Imports System.Threading

' Request permission to set thread principal.
<Assembly: SecurityPermissionAttribute( _
    SecurityAction.RequestOptional, ControlPrincipal := True)>

Public Class Principal

    <MTAThread> _
    Shared Sub Main()

        Dim rolesArray As String() = {"managers", "executives"}
        Try
            ' Set the principal to a new generic principal.
            Thread.CurrentPrincipal = _
                New GenericPrincipal(New GenericIdentity( _
                "Bob", "Passport"), rolesArray)

        Catch secureException As SecurityException
            Console.WriteLine("{0}: Permission to set Principal " & _
                "is denied.", secureException.GetType().Name)
        End Try

        Dim threadPrincipal As IPrincipal = Thread.CurrentPrincipal
        Console.WriteLine( _
            "Name: {0}" & vbCrLf & "IsAuthenticated:" & _
            " {1}" & vbCrLf & "AuthenticationType: {2}", _
            threadPrincipal.Identity.Name, _
            threadPrincipal.Identity.IsAuthenticated, _
            threadPrincipal.Identity.AuthenticationType)

    End Sub
End Class


.NET Framework Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Community Content

This is horrible
Added by:Greg P
There is no descritption of this property, there is only code?!?
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View