Home All Groups Group Topic Archive Search About

Detect if a local user/group account exists

Author
25 Apr 2006 10:30 AM
Roshan
Hi,

Given a name, I want to be able to detect if a user or a group account
exists with that name on a system and know if its a user or a group. My
search yielded the "LookupAccountName" Win32 API but I haven't found
any class in .NET that allows me to do that.

Does any one know of a class in .NET or a way of doing this without
using Win32 APIs ?

Thanks,
Roshan Achar

Author
25 Apr 2006 11:00 AM
Henning Krause [MVP]
Hello,

I think, a quick hack would be this:

public bool GroupExists(string name) {
  try
  {
    NTAccount account = new NTAccount(name);
    acc.Translate(typeof(SecurityIdentifier));
    return true;
  }
  catch (PrincipalNotMappedException)
  {
    return false;
  }
}

Not very elegant, though.

Best regards,
Henning Krause

Show quote
"Roshan" <bros***@gmail.com> wrote in message
news:1145961035.874742.218230@u72g2000cwu.googlegroups.com...
> Hi,
>
> Given a name, I want to be able to detect if a user or a group account
> exists with that name on a system and know if its a user or a group. My
> search yielded the "LookupAccountName" Win32 API but I haven't found
> any class in .NET that allows me to do that.
>
> Does any one know of a class in .NET or a way of doing this without
> using Win32 APIs ?
>
> Thanks,
> Roshan Achar
>
Author
26 Apr 2006 4:24 AM
Roshan
Thanks Henning.

This will suffice for the moment. It would have been better if an API
existed to do this directly.Also the Exception thrown is
IdentityNotMappedException
I found that I can detect if the name is a user or a group using

SecurityIdentifier.IsAccountSid(). It returns true if the name is a
local user account and false if its a group.

Thanks,
Roshan

Henning Krause [MVP] wrote:

Show quote
> Hello,
>
> I think, a quick hack would be this:
>
> public bool GroupExists(string name) {
>   try
>   {
>     NTAccount account = new NTAccount(name);
>     acc.Translate(typeof(SecurityIdentifier));
>     return true;
>   }
>   catch (PrincipalNotMappedException)
>   {
>     return false;
>   }
> }
>
> Not very elegant, though.
>
> Best regards,
> Henning Krause
>
> "Roshan" <bros***@gmail.com> wrote in message
> news:1145961035.874742.218230@u72g2000cwu.googlegroups.com...
> > Hi,
> >
> > Given a name, I want to be able to detect if a user or a group account
> > exists with that name on a system and know if its a user or a group. My
> > search yielded the "LookupAccountName" Win32 API but I haven't found
> > any class in .NET that allows me to do that.
> >
> > Does any one know of a class in .NET or a way of doing this without
> > using Win32 APIs ?
> >
> > Thanks,
> > Roshan Achar
> >
Author
25 May 2006 4:09 PM
Knut Wehrle
Hi Henning,

I've been looking for this for a long time. But it solves just one problem
for me.
I want to test if a group exists, and if not, I want to create it.

How can i do this?

Any idea?


Show quote
"Henning Krause [MVP]" <newsgroups.rem***@this.infinitec.de> schrieb im
Newsbeitrag news:uAZ09dFaGHA.3524@TK2MSFTNGP04.phx.gbl...
> Hello,
>
> I think, a quick hack would be this:
>
> public bool GroupExists(string name) {
>  try
>  {
>    NTAccount account = new NTAccount(name);
>    acc.Translate(typeof(SecurityIdentifier));
>    return true;
>  }
>  catch (PrincipalNotMappedException)
>  {
>    return false;
>  }
> }
>
> Not very elegant, though.
>
> Best regards,
> Henning Krause
>
> "Roshan" <bros***@gmail.com> wrote in message
> news:1145961035.874742.218230@u72g2000cwu.googlegroups.com...
>> Hi,
>>
>> Given a name, I want to be able to detect if a user or a group account
>> exists with that name on a system and know if its a user or a group. My
>> search yielded the "LookupAccountName" Win32 API but I haven't found
>> any class in .NET that allows me to do that.
>>
>> Does any one know of a class in .NET or a way of doing this without
>> using Win32 APIs ?
>>
>> Thanks,
>> Roshan Achar
>>
>
>
Author
16 Jun 2006 10:29 AM
Roshan
Hi Knut,

You can create a group as follows

            using (DirectoryEntry AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer"))
            {
                try
                {
                    String typeName = "group";
                    String accountName = "KnutGroup";


                    using (DirectoryEntry NewUser =
AD.Children.Add(accountName, typeName))
                    {
                        NewUser.CommitChanges();
                    }
                }
                catch
                {
                    Console.WriteLine("Error occured");
                }
            }

Hope this suffices,

Thanks,
Roshan

Knut Wehrle wrote:
Show quote
> Hi Henning,
>
> I've been looking for this for a long time. But it solves just one problem
> for me.
> I want to test if a group exists, and if not, I want to create it.
>
> How can i do this?
>
> Any idea?
>
>
> "Henning Krause [MVP]" <newsgroups.rem***@this.infinitec.de> schrieb im
> Newsbeitrag news:uAZ09dFaGHA.3524@TK2MSFTNGP04.phx.gbl...
> > Hello,
> >
> > I think, a quick hack would be this:
> >
> > public bool GroupExists(string name) {
> >  try
> >  {
> >    NTAccount account = new NTAccount(name);
> >    acc.Translate(typeof(SecurityIdentifier));
> >    return true;
> >  }
> >  catch (PrincipalNotMappedException)
> >  {
> >    return false;
> >  }
> > }
> >
> > Not very elegant, though.
> >
> > Best regards,
> > Henning Krause
> >
> > "Roshan" <bros***@gmail.com> wrote in message
> > news:1145961035.874742.218230@u72g2000cwu.googlegroups.com...
> >> Hi,
> >>
> >> Given a name, I want to be able to detect if a user or a group account
> >> exists with that name on a system and know if its a user or a group. My
> >> search yielded the "LookupAccountName" Win32 API but I haven't found
> >> any class in .NET that allows me to do that.
> >>
> >> Does any one know of a class in .NET or a way of doing this without
> >> using Win32 APIs ?
> >>
> >> Thanks,
> >> Roshan Achar
> >>
> >
> >
Author
2 Jul 2006 11:34 AM
Knut Wehrle
Hi Roshan,

that's great!!! Thank you!
I've been looking for the solution very long.

Best Regards
Knut



Show quote
"Roshan" <bros***@gmail.com> schrieb im Newsbeitrag
news:1150453776.439278.228500@r2g2000cwb.googlegroups.com...
> Hi Knut,
>
> You can create a group as follows
>
>            using (DirectoryEntry AD = new DirectoryEntry("WinNT://" +
> Environment.MachineName + ",computer"))
>            {
>                try
>                {
>                    String typeName = "group";
>                    String accountName = "KnutGroup";
>
>
>                    using (DirectoryEntry NewUser =
> AD.Children.Add(accountName, typeName))
>                    {
>                        NewUser.CommitChanges();
>                    }
>                }
>                catch
>                {
>                    Console.WriteLine("Error occured");
>                }
>            }
>
> Hope this suffices,
>
> Thanks,
> Roshan
>
> Knut Wehrle wrote:
>> Hi Henning,
>>
>> I've been looking for this for a long time. But it solves just one
>> problem
>> for me.
>> I want to test if a group exists, and if not, I want to create it.
>>
>> How can i do this?
>>
>> Any idea?
>>
>>
>> "Henning Krause [MVP]" <newsgroups.rem***@this.infinitec.de> schrieb im
>> Newsbeitrag news:uAZ09dFaGHA.3524@TK2MSFTNGP04.phx.gbl...
>> > Hello,
>> >
>> > I think, a quick hack would be this:
>> >
>> > public bool GroupExists(string name) {
>> >  try
>> >  {
>> >    NTAccount account = new NTAccount(name);
>> >    acc.Translate(typeof(SecurityIdentifier));
>> >    return true;
>> >  }
>> >  catch (PrincipalNotMappedException)
>> >  {
>> >    return false;
>> >  }
>> > }
>> >
>> > Not very elegant, though.
>> >
>> > Best regards,
>> > Henning Krause
>> >
>> > "Roshan" <bros***@gmail.com> wrote in message
>> > news:1145961035.874742.218230@u72g2000cwu.googlegroups.com...
>> >> Hi,
>> >>
>> >> Given a name, I want to be able to detect if a user or a group account
>> >> exists with that name on a system and know if its a user or a group.
>> >> My
>> >> search yielded the "LookupAccountName" Win32 API but I haven't found
>> >> any class in .NET that allows me to do that.
>> >>
>> >> Does any one know of a class in .NET or a way of doing this without
>> >> using Win32 APIs ?
>> >>
>> >> Thanks,
>> >> Roshan Achar
>> >>
>> >
>> >
>

AddThis Social Bookmark Button