Keeping HTTP Basic Authentification alive while being redirected

后端 未结 2 756
旧时难觅i
旧时难觅i 2021-01-12 13:42

We are using web service with basic authentication. It all worked all fine, till owners of web service implemented balancing service. Which is simply redirects requests to

2条回答
  •  执念已碎
    2021-01-12 14:46

    Indeed, CredentialCache is working correctly. However, if you would like to add multiple basic auth credentials (for example if there is redirection that you are aware of) you can use following function that I have made:

    private void SetNetworkCredential(Uri uriPrefix, string authType, NetworkCredential credential)
    {
        if (request.Credentials == null)
        {
            request.Credentials = new CredentialCache();
        }
    
        if (request.Credentials.GetCredential(uriPrefix, authType) == null)
        {
            (request.Credentials as CredentialCache).Add(uriPrefix, authType, credential);
        }
    }
    

    I hope it will help somebody in the future.

提交回复
热议问题