0

I have the following pattern throughout my application, creating a Server object using impersonation to retrieve data.

public ActionResult Index(){
    Index model = new Index(); 

    using (new Impersonation("domain", "username", "password")){
            Server server = new Server("serverInstance"); 

            model.ApplicationList = server.GetApplications();
            model.Details = server.GetDetails(); 
    }
}

I was wondering if this can be translated into a wrapper function. So it can be called the following way

SecureManager.PerformOperation("domain", "username", "password", server => server 
{
    server.GetApplications();
    server.GetDetails();
    ....
});

The goal is to only use the Server object within the Impersonation block.

1 Answer 1

1

Yes, you can do that.

void /*SecureManager.*/PerformOperation(
   string domain, string username, string password, Action<Server> action)
{
   using (new Impersonation(domain, username, password))
   {
            action(new Server("serverInstance")); 
   }
}

But it would not really solve "only use the Server object within the Impersonation block" because that object can be easily "leaked" out of the lambda:

  Server leakedServer = null;
  SecureManager.PerformOperation("domain", "username", "password", 
      server => 
      {
         leakedServer = server;  
         ....
       });
  leakedServer.GetApplications();
Sign up to request clarification or add additional context in comments.

1 Comment

Would you mind sharing the syntax on how to do it? I'm fine with it as long as it gets instantiated in the PerformOperation method, under Impersonation block @Alexei Levenkov

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.