how to set the Principal policy to WindwosPrincipal ?

loyal

Centurion
Joined
Jul 29, 2003
Messages
102
hi all I am practising on MCAD training kit book

and here's an example I tried but gave me an exception

specified cast is not valid

and here's the code as an example in my book

Code:
' This example assumes that the Principal policy has been set to 
' WindowsPrincipal.
Dim myPrincipal As WindowsPrincipal
' Gets a reference to the current WindowsPrincipal
myPrincipal = CType(Threading.Thread.CurrentPrincipal, _
   WindowsPrincipal)
Dim myIdentity As WindowsIdentity
' Gets the WindowsIdentity of the current principal
myIdentity = CType(myPrincipal.Identity, WindowsIdentity)
' Displays the username of the current user
MessageBox.Show(myIdentity.Name)
 
There is no valid cast from a GenericPrinciple (which Threading.Thread.CurrentPrincipal is) and a WindowsPrinciple.

They both implement the same interface (IPrinciple) so either could be casted to IPrinciple, neither could be casted to the other.

The following should work
Visual Basic:
' This example assumes that the Principal policy has been set to 
' WindowsPrincipal.
Dim myPrincipal As IPrincipal
' Gets a reference to the current WindowsPrincipal
myPrincipal = DirectCast(Threading.Thread.CurrentPrincipal, _
   IPrincipal)
Dim myIdentity As IIdentity
' Gets the WindowsIdentity of the current principal
myIdentity = DirectCast(myPrincipal.Identity, IIdentity)
' Displays the username of the current user
MessageBox.Show(myIdentity.Name)
 
yeah thanks

but I found this script later in my book to set the

Principal Policy To WindowsPrincipal

AppDomain.CurrentDomain.SetPrincipalPolicy(Security.Principal.PrincipalPolicy.WindowsPrincipal)
 
Back
Top