De HTTP/2-client ondersteunt 2 authenticatietypen: Basisauthenticatie en OAuth2-authenticatie.
Gebruik de gebeurtenis OnHTTP2Authorization om beide typen authenticatie af te handelen.
Als de server een header retourneert die Basic Authenticatie vereist, stelt u in OnHTTP2Authorization de gebruikersnaam en het wachtwoord in.
TsgcHTTP2Client *oClient = new TsgcHTTP2Client();
oClient->OnHTTP2Authorization = OnHTTP2AuthorizationEvent;
...
void OnHTTP2AuthorizationEvent(TObject *Sender, const TsgcHTTP2ConnectionClient *Connection, const string AuthType, const string AuthData, string &UserName, string &Password, string &aToken, bool &Handled)
{
if (AuthType == "Basic")
{
UserName = "user";
Password = "secret";
}
}
Als de server een koptekst retourneert die Bearer Token-authenticatie vereist, stel dan het token in via OnHTTP2Authorization.
TsgcHTTP2Client *oClient = new TsgcHTTP2Client();
oClient->OnHTTP2Authorization = OnHTTP2AuthorizationEvent;
...
void OnHTTP2AuthorizationEvent(TObject *Sender, const TsgcHTTP2ConnectionClient *Connection, const string AuthType, const string AuthData, string &UserName, string &Password, string &aToken, bool &Handled)
{
if (AuthType == "Bearer")
{
aToken = "bearer token";
}
}
Als u de Bearer-waarde al kent omdat u deze via een andere methode hebt verkregen, kunt u de Bearer-waarde als HTTP-header doorgeven via de volgende eigenschappen van het verzoek; stel deze in voordat u een HTTP-verzoeksmethode aanroept:
TsgcHTTP2Client.Request.BearerAuthentication = trueTsgcHTTP2Client.Request.BearerToken = "< value of the token >"
Lees het volgende artikel als u ons OAuth2-component met HTTP/2-client wilt gebruiken.