enumerate roles a user belongs to ? (seek vb.net sample)

M

Mad Scientist Jr

I would like to write a vb.asp.net function

Private Function fn_sGetRoles(byval sDelimiter as string) As
String

that simply returns a delimited list of all the roles a user belongs
to.

I did some research, and found a number of solutions, that all seem to
use a WindowsIdentity type object. However, when I try to convert
these to vb.net, on a line like

Dim wi As WindowsIdentity

I get this error

type 'WindowsIdentity' is not defined

What do I need to import for this?

Can someone post complete code to enumerate all the roles the given
user visiting my intranet page belongs to?

Thanks

Below are the 3 examples I found that supposedly do this, which I
can't get working:

'EXAMPLE #1:
'AppDomain domain = Thread.GetDomain();
'domain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
'WindowsPrincipal principal =
(WindowsPrincipal)Thread.CurrentPrincipal;
'WindowsIdentity identity =
(WindowsIdentity)principal.Identity;
'Type idType;
'idType = identity.GetType();
'Object result = idType.InvokeMember(
' "_GetRoles",
' BindingFlags.Static | BindingFlags.NonPublic |
BindingFlags.InvokeMethod,
' null,
' identity,
' new object[]{identity.Token},
' null);

'string[] roles = (string[]) result;
'for (int i=0; i<roles.Length; i++)
' Console.WriteLine("Role: {0}", roles);





'EXAMPLE #2:
'Dim wp As WindowsPrincipal = HttpContext.Current.User
'Dim id As WindowsIdentity = wp.Identity
'Dim idType As Type
'idType = GetType(WindowsIdentity)
'Dim result As Object = idType.InvokeMember("_GetRoles",
BindingFlags.Static Or BindingFlags.InvokeMethod Or
BindingFlags.NonPublic, Nothing, id, New Object() {id.Token}, Nothing)
'Dim roles() As String = DirectCast(result, String())
'Dim i As Integer
'For i = 0 To roles.Length - 1
' Response.Write("<br>" + roles(i))
'Next





'EXAMPLE #3:
'WindowsIdentity wi=WindowsIdentity.GetCurrent();
'Type typeWi=wi.GetType();
'try
'{
' string[] roles=(string[])typeWi.InvokeMember ("GetRoles",
'BindingFlags.NonPublic |
BindingFlags.InvokeMethod|BindingFlags.Instance,
'null,wi, new object [] {});
' foreach(string role in roles)
' Console.WriteLine (role);
' }
' catch(Exception ex)
' {
' Console.WriteLine(ex.Message);
' }
'}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,525
Latest member
RosalindSa

Latest Threads

Top