locked
Block Windows "Guest" account RRS feed

  • Question

  • Hi,

    I want to restrict users log on the computer from Windows "Guest" account from starting our application. Our application can be used on XP, Vista and Windows 7.
    What is wrong with this solution, I get SecurityException when testing from Win7 guest account, I have tried both PrincipalPermission(null, "Guests"); and PrincipalPermission(null, "Guest");
    I have also tried to use PrincipalPermission(null, "Administrators"); and then I can use the application from Admin account but not from guest account.

    static void Main()
    {
    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
    PrincipalPermission principalGuest = new PrincipalPermission(null, "Guest"); //Has also tested with "Guests"

    try
    {
    principalGuest.Demand();
    }
    catch (SecurityException e)
    {
    //I only want to get this exception if the user is Windows account type "guest"
    }
    }

    Best regards
    Olof
    Tuesday, February 1, 2011 8:01 AM

Answers

  •  you can use user name  to do this      

    string username = Environment.UserDomainName;
                if (username.ToLower() == "guest")
                    Application.Exit();


    Please mark as answer if it is answer for you problem

    Bimbim.in

    bimbim.in

    • Marked as answer by OlofC Wednesday, February 2, 2011 9:18 AM
    Tuesday, February 1, 2011 8:12 AM
  • I know, but the guest account is member of the guest group.
    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Marked as answer by OlofC Wednesday, February 2, 2011 9:18 AM
    Wednesday, February 2, 2011 8:42 AM

All replies

  •  you can use user name  to do this      

    string username = Environment.UserDomainName;
                if (username.ToLower() == "guest")
                    Application.Exit();


    Please mark as answer if it is answer for you problem

    Bimbim.in

    bimbim.in

    • Marked as answer by OlofC Wednesday, February 2, 2011 9:18 AM
    Tuesday, February 1, 2011 8:12 AM
  • Thanks.

    But you mean Environment.UserName? One problem is that the application can be used on many different language OS, if we are using chinese OS do we have to translate "guest" to chinese before or is there another way to determine this?

    We can translate "guest" but it is easy if there is another way to solve it.

    Tuesday, February 1, 2011 2:08 PM
  • Hi,

    you can use the isInRole() method like:


    //Visual C# 2008 - .net 3.5 - Any CPU
          AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
          WindowsPrincipal principal = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal;
          if (principal.IsInRole(WindowsBuiltInRole.Guest))
          {
            // etc.
          }
    

     


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Tuesday, February 1, 2011 2:37 PM
  • Hi Heslacher,

    I mean this guest account http://www.sevenforums.com/tutorials/7412-guest-account-turn-off.html

    WindowsBuiltInRole.Guest is the Guest gruop.

    Wednesday, February 2, 2011 8:25 AM
  • I know, but the guest account is member of the guest group.
    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Marked as answer by OlofC Wednesday, February 2, 2011 9:18 AM
    Wednesday, February 2, 2011 8:42 AM