For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET > August 2004 > enumerate roles a user belongs to ? (seek vb.net sample)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author enumerate roles a user belongs to ? (seek vb.net sample)
Mad Scientist Jr

2004-08-31, 4:01 pm

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[i]);





'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);
' }
'}
Karl

2004-08-31, 4:01 pm

WindowsIdentity is located in : System.Security.Principal

Karl

"Mad Scientist Jr" <usenet_daughter@yahoo.com> wrote in message
news:7a93f3c4.0408310901.3acbd76f@posting.google.com...
> 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[i]);
>
>
>
>
>
> '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);
> ' }
> '}



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com