I am currently building an authentication interface for a 3rd parting API. I have a class called FooCredential, when the class is destructed, it needs to send a post request to the auth server to revoke the current token, this is done through an async function.
My problem is that if I want the token to be automatically revoked, I have to put the method call in the finalizer. However, I can't seem to find a way that would allow me to wait on the async call to complete. And without waiting, the call would not be complete before the main thread exit, therefore leaving the token alive.
I tried many methods to wait, including spawning a new thread in the finalizer to run a static version of the async revoke method. The thread does not seem to be executing at all in the debugger, the breakpoint does not get hit. Coming from C++, this is very strange. I also tried to implement the IDisposable, does not seems to be working too.
No IAsyncDisposable since I'm stuck on .NET framework 4.7.2.
I am completely lost at the moment. Any help is appreciated.
class FooCredential
{
public string Token { get; set; }
~FooCredential()
{
if (Token != null)
{
var data = new Dictionary<string, string>
{
{"token", Token}
};
Thread t = new Thread(RevokeTokenStatic);
t.Start(data);
t.Join();
}
}
private static async void RevokeTokenStatic(object param)
{
var data = (Dictionary<string, string>) param;
string token = data["token"];
var body = new Dictionary<string, string>
{
{"token", token}
};
var content = new FormUrlEncodedContent(body);
await new HttpClient().PostAsync(AuthBaseUrl + "/oauth/revoke", content);
}
}