mattjohnsonpint/SimpleImpersonation

Visual Studio 2022 Warning for RunImpersonatedAsync

KayakFisher205 opened this issue · 3 comments

Microsoft Visual Studio Professional 2022 (64-bit)
Version 17.0.4
SimpleImpersonation version 4.0

Using the code posted on the homepage, I'm getting a warning from visual studio below, along with a screenshot. The code works fine, so I'm not sure if its an Visual Studio 2022 issue or some other issue.
I just wanted to see if I needed to change my code or just ignore it for now.

Warning CS1998 This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
warning for impersonation

Hi. The warning is correct. The code you are running is not asynchronous. To clear the warning, you should use RunImpersonated rather than RunImpersonatedAsync.

Isn't that the same as the readme.md file on the homepage?

await WindowsIdentity.RunImpersonatedAsync (userHandle, async () => {
// do whatever you want as this user.
});
Or, must the code run inside the Impersonation also be awaited/Ansychronous? Maybe that's where I'm misunderstanding.

There are four examples in the Readme. The first two are for synchronous code. The second two are for async code.

If the code you're running inside the body of the function needs to be run asynchronously, then use async/await with RunImpersonatedAsync. Otherwise, use RunImpersonated.