emanzione / PATCH

The PATCH repository for issues tracking, wiki and shared material.
https://assetstore.unity.com/packages/tools/utilities/p-a-t-c-h-ultimate-patching-system-41417
MIT License
47 stars 7 forks source link

Make Credentials usage more flexible #61

Closed DmitriyYukhanov closed 1 year ago

DmitriyYukhanov commented 1 year ago

Hey, I'd like to propose 2 improvements into how Credentials are handled right now:

  1. Use ICredentials instead of NetworkCredential. It would allow passing the CredentialCache which may be required in some cases, for example.

  2. Optionally, allow sending Credentials with request right away.

By default, Credentials will send only after server return 401 status code.
But it's often a good idea to reduce bandwidth and requests count when dealing with CDNs which do require auth, so it's preferable to send the Credentials with the initial request instead.

It's possible to achieve by extending WebClient (if you will not switch to HttpClient) and setting WebRequest.PreAuthenticate to True, like it's shown in this Microsoft Learn example.

Alternatively, it's possible to do this with manually constructing and adding the HttpRequestHeader.Authorization header from the Credentials right to the initial request. This will be possible after #59 implementation, but still would be nice to just have an option to do this, like:

context.Downloader.Credentials = myNetworkCredential;
context.Downloader.PreAuthenticate = true;

and allow it to do all the rest under the hood.

emanzione commented 1 year ago

All IDownloader-derived downloaders now expose credentials as ICredentials.

Also, all WebRequests created by the downloaders now have PreAuthenticated flag set. For the sake of completeness, I introduced PreAuthenticatedWebClient class that always returns a PreAuthenticated WebRequest.

This will be released with the next version!