loyal Posted August 17, 2003 Posted August 17, 2003 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 ' 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) Quote Gary Says: To be a good programmer, you must be good at debugging
Administrators PlausiblyDamp Posted August 17, 2003 Administrators Posted August 17, 2003 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 ' 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) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
loyal Posted August 17, 2003 Author Posted August 17, 2003 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) Quote Gary Says: To be a good programmer, you must be good at debugging
*Gurus* Derek Stone Posted August 17, 2003 *Gurus* Posted August 17, 2003 CurrentPrincipal is defined as IPrincipal, not GenericPrincipal as stated above. It can easily accept any class that implements IPrincipal. Quote Posting Guidelines
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.