Re: How to get user company

From: Morten Wennevik (MortenWennevik_at_hotmail.com)
Date: 06/21/04


Date: Mon, 21 Jun 2004 07:56:00 +0200

Hi Son,

I believe you mean User Group or User Role. All that stuff is in System.Security.Principal

You can get a WindowsIdentity from the user running the code with

WindowsIdentity wi = WindowsIdentity.GetCurrent();

Then you can get all roles for that identity by using Type.InvokeMember, cleverly demonstrated by Joe Kaplan and translated to C# by Chris Taylor
http://dotnetjunkies.com/WebLog/chris.taylor/archive/2004/02/25/7945.aspx

public static string[] GetWindowsIdentityRoles( WindowsIdentity identity )
{
        object result = typeof(WindowsIdentity).InvokeMember( "_GetRoles",
                BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.NonPublic,
                null, identity, new object[]{identity.Token}, null );
                return (string[])result;
}

You can also test if a user is a member of a specific role like BUILTIN\Administrators

WindowsPrincipal wp = new WindowsPrincipal(wi); // wi = WindowsIdentity.GetCurrent()

if(wp.IsInRole("BUILTIN\\Administrators"))
        // someone with administrator rights

-- 
Happy coding!
Morten Wennevik [C# MVP]


Relevant Pages

  • Re: Windows Identity simple question ????
    ... Dominick Baier ... Developing More Secure Microsoft ASP.NET 2.0 Applications ... WindowsIdentity identity = ...
    (microsoft.public.dotnet.security)
  • Re: Windows Identity simple question ????
    ... WindowsIdentity identity = System.Threading.Thread.CurrentPrincipal.Identity ... which use services from a remote component host in a windows service. ... Windows service instead the client context. ...
    (microsoft.public.dotnet.security)
  • Re: Other than BuiltIn groups for Windows Authentication
    ... Web config ... WindowsIdentity identity = winPrincipal.Identity as WindowsIdentity; ... >> I am using windows authentication on my ASP.Net application. ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • whoami?
    ... WindowsIdentity identity = ... "identity.Name" holds my username as user ... If at the same time I'm logged in to a W200x server with a ...
    (microsoft.public.dotnet.security)
  • Re: refreshing windowsidentity for user group changes
    ... Are you saying that you use the WindowsIdentity constructor that just takes ... Joe Kaplan-MS MVP Directory Services Programming ... Co-author of "The .NET Developer's Guide to Directory Services Programming" ... contains the user group that the user was removed from. ...
    (microsoft.public.dotnet.framework.aspnet.security)