Jump to content
Xtreme .Net Talk

how to set the Principal policy to WindwosPrincipal ?


Recommended Posts

Posted

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)



Gary Says: To be a good programmer, you must be good at debugging
  • Administrators
Posted

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)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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)

Gary Says: To be a good programmer, you must be good at debugging

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...