I'm trying to design a library (nuget package) that would read data from a REST API and deserialize it to DTO objects. The API requires authentication which is usually done with Microsoft's ADAL library like so:

var authenticationContext = new AuthenticationContext(authority);
            ClientCredential clientcred = new ClientCredential(clientId, clientSecret);
            AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(uri, clientcred).Result;

I would like to design the library so that the user would only need to pass the access token. I considered event for this, but as you can see from the example code, the AcquireTokenAsync is an async method, so events are out of question.

I was planning just to expose an event from the main class:

event EventHandler<SendingRequestEventArgs> SendingRequest;

So that the user could write:

    if (Auth != null)
        AuthenticationResult token = Auth.AcquireToken();
        if (token != null)
            requestMessage.Headers.Add("Authorization", new AuthenticationHeaderValue("Bearer", token.AccessToken).ToString());

Are there any other design patterns for this kind of extension points that support async pattern?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.