locked
Running application as current logged in user RRS feed

  • Question

  • Hi,

    I have a machine accessible by domain windows users. The machine has a WPF application. User can launch WPF application.

    The application does some processing which required currently logged in user. The problem is application doesn't run in context of currently logged in user.

    How to make the application run in context of logged in windows user?

    Friday, May 30, 2014 10:28 AM

Answers

  • Why doesn't running this application run it in the context of the currently logged in user?

    Here I am logged in on this machine.
    I run an exe.

    It will run in the context I am logged on in.

    Friday, May 30, 2014 11:30 AM
  • What exactly do you mean by "the application doesn't run in context of currently logged in user"? A standalone WPF application executes with unrestricted permissions. If you want to execute some method in the context of a specific user, you should use impersonation:

    System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
                string userName = identity.Name;
    
                System.Security.Principal.WindowsImpersonationContext impersonationContext = null;
                try
                {
                    impersonationContext = identity.Impersonate();
    
                   //call any methods under the context of the current user here...
                }
                finally
                {
                    if (impersonationContext != null)
                    {
                        impersonationContext.Undo();
                    }
                }
    

    Friday, May 30, 2014 11:36 AM

All replies

  • Why doesn't running this application run it in the context of the currently logged in user?

    Here I am logged in on this machine.
    I run an exe.

    It will run in the context I am logged on in.

    Friday, May 30, 2014 11:30 AM
  • What exactly do you mean by "the application doesn't run in context of currently logged in user"? A standalone WPF application executes with unrestricted permissions. If you want to execute some method in the context of a specific user, you should use impersonation:

    System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
                string userName = identity.Name;
    
                System.Security.Principal.WindowsImpersonationContext impersonationContext = null;
                try
                {
                    impersonationContext = identity.Impersonate();
    
                   //call any methods under the context of the current user here...
                }
                finally
                {
                    if (impersonationContext != null)
                    {
                        impersonationContext.Undo();
                    }
                }
    

    Friday, May 30, 2014 11:36 AM
  • Hi,

    Since we haven't heard from you for a long time, I temporarily close this case. I mark useful reply as answer. If you have any concerns, please free feel to reopen it or submit a new question.

    Thanks for your understanding.

    Best regards,
    Franklin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 6, 2014 9:36 AM