none
I have a C# application that runs with elevated permission. Is there a way to open the application with minimum functionalities when the user selects "No" on the UAC prompt. RRS feed

  • Question

  • To run application with administrator rights, the values are set in application.Manifest file.

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>

    This helps in launching the application with full administrative access. It prompts for user consent.

    When user selects NO, the application closes. Is there a way to open the application with minimum functionalities instead of closing the application.


    Friday, November 22, 2019 4:07 PM

Answers

  • If the user opts to cancel the request then the process doesn't start. This is by design. The correct approach is to break your app into "admin" and non-admin areas. Start your app normally for non-admin stuff. Use the standard UAC shield to indicate functionality that requires elevation. When the user clicks on that then elevate the process which (unfortunately) requires the app to restart but now the user has admin access. The workflow is defined here.

    The steps for doing all this is documented but I cannot find it off the top of my head. There are a few pieces to the puzzle though. The first thing your app needs to do is determine if the user is an admin. You can do this by simply looking at their roles. Code for this is available everywhere.

    If the user is an admin then you don't need to do anything else, just run your app normally. If the user is not an admin then you'll want to add the UAC icon to any button that needs elevation. This is just a message you send to the button. You can see some sample C# code here.

    When the user clicks a button that requires admin privileges you need to determine if they are an admin or not. If they aren't then you need to trigger elevation. This post talks about how to runas with restart of your app.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Ranjini Rengan Monday, November 25, 2019 8:05 PM
    Friday, November 22, 2019 6:58 PM
    Moderator

All replies

  • I think no but I also think it is possible for the application to begin with non-elevated privileges then elevate the privileges when needed. I do not know how to do that but I could figure it out if I needed to. Try looking for that.


    Sam Hobbs
    SimpleSamples.Info

    Friday, November 22, 2019 4:34 PM
  • Thank you Sam. I will try the option you suggested.
    Friday, November 22, 2019 6:11 PM
  • If the user opts to cancel the request then the process doesn't start. This is by design. The correct approach is to break your app into "admin" and non-admin areas. Start your app normally for non-admin stuff. Use the standard UAC shield to indicate functionality that requires elevation. When the user clicks on that then elevate the process which (unfortunately) requires the app to restart but now the user has admin access. The workflow is defined here.

    The steps for doing all this is documented but I cannot find it off the top of my head. There are a few pieces to the puzzle though. The first thing your app needs to do is determine if the user is an admin. You can do this by simply looking at their roles. Code for this is available everywhere.

    If the user is an admin then you don't need to do anything else, just run your app normally. If the user is not an admin then you'll want to add the UAC icon to any button that needs elevation. This is just a message you send to the button. You can see some sample C# code here.

    When the user clicks a button that requires admin privileges you need to determine if they are an admin or not. If they aren't then you need to trigger elevation. This post talks about how to runas with restart of your app.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Ranjini Rengan Monday, November 25, 2019 8:05 PM
    Friday, November 22, 2019 6:58 PM
    Moderator
  • The archived MS Sample (my previously posted link) illustrates how to restart a C# process with elevation and the use of the UAC shield.
    Friday, November 22, 2019 8:36 PM
  • Thank you @CoolDadTx for the detailed explanation and the links.
    Monday, November 25, 2019 8:04 PM