authenticationRequired signal not emited or slot not called
-
Hallo there,
I am having a strange behavior with QNetworkAccessManager. I am getting a QNetworkReply::NetworkError(AuthenticationRequiredError) but the authenticationRequired signal seems not getting emited. The Server is throwing a 401. Any ideas what is wrong?
I am calling it like:
manager->get(request); connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*))); connect(manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),this, SLOT(auth(QNetworkReply*,QAuthenticator*)));
-
@gollum
IIRC the authenticationRequired signal will be emitted when the HTTP response contains a WWW-Authenticate header.
HTTP401 is just an error code that you are not allowed to view this resource. So the webserver/webapplication doesn't even ask you to authenticate. -
@raven-worx I have the same problem in Qt 6.6.1 that the authenticationRequired signal is not emitted.
When I debug the header sent from the server, I see that it indeed contains a www-authenticate response:
Reply: QList(std::pair("server","Microsoft-IIS/10.0"), std::pair("www-authenticate","Negotiate, NTLM"), std::pair("x-powered-by","ASP.NET"), std::pair("date","Mon, 29 Jan 2024 08:58:41 GMT"))
So in my understanding this should trigger the signal. Here is my code:
NTLMexample::NTLMexample(QObject *parent) : QObject{parent} { connect(&manager, &QNetworkAccessManager::authenticationRequired, this, &NTLMexample::authenticationRequired); connect(&manager, &QNetworkAccessManager::preSharedKeyAuthenticationRequired,this, &NTLMexample::preSharedKeyAuthenticationRequired); connect(&manager, &QNetworkAccessManager::proxyAuthenticationRequired,this, &NTLMexample::proxyAuthenticationRequired); connect(&manager, &QNetworkAccessManager::finished, this, &NTLMexample::requestFinished); } void NTLMexample::requestFinished(QNetworkReply *reply) { if (reply->error() == QNetworkReply::NoError) { qInfo() << "Request successful!"; qInfo() << "Response:" << reply->readAll(); } else { qInfo() << "Error:" << reply->errorString(); qInfo() << "Reply:" << reply->rawHeaderPairs(); } } void NTLMexample::preSharedKeyAuthenticationRequired(QNetworkReply *reply, QSslPreSharedKeyAuthenticator *authenticator) { qInfo() << "preSharedKeyAuthenticationRequired"; } void NTLMexample::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator) { qInfo() << "proxyAuthenticationRequired"; } void NTLMexample::authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator) { // NTLM authentication credentials qInfo() << "authenticationRequired signal emitted!"; } void NTLMexample::makeRequest(QString URL) { // Set up the request QNetworkRequest request = QNetworkRequest(QUrl(URL)); // Make the request QNetworkReply *reply = manager.get(request); }
-
@Depronized Resuscitating an 8-year-old thread for a new problem: not quite a record.
What is the scope of the
manager
object? -
@ChrisW67 Yes maybe I should have started a new thread, but since the problme description was the exact same and this wasn't answered, I figured it was better to continue it. Anyway, my manager object is declared in the header file of this class.
#ifndef NTLMEXAMPLE_H #define NTLMEXAMPLE_H #include <QObject> #include <QCoreApplication> #include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> #include <QAuthenticator> #include <QDebug> class NTLMexample : public QObject { Q_OBJECT public: explicit NTLMexample(QObject *parent = nullptr); public slots: void requestFinished(QNetworkReply *reply); void authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator); void makeRequest(QString URL); void preSharedKeyAuthenticationRequired(QNetworkReply *reply, QSslPreSharedKeyAuthenticator *authenticator); void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator); private: QNetworkAccessManager manager; };
-
Are you you using an instance of this class to process the request?
Is that object staying in scope for the life of the request and response?
Exactly what qDebug() output do you see processing the request?
Do the encrypted() or sslErrors() signals get emitted?What client platform?
Public site? -
@ChrisW67 Huge thanks for taking your time. Yes I create one instance and call makeRequest with an URL.
I have verified the URL so I know that it is correct. The server is on our corporate network and my machine is logged in to the domain which is required in order to access this server. I used the Postman Agent to be able to test it, there I chose NTLM authentication and the credentials and it worked fine.
When I try the same URL in Qt, the only signals that are emitted are finished and encrypted. There is no sslError. Debugging reply->readAll returns empty, but rawHeaderPairs return the result you see in my first post in this thread. That was how I figured out that the server actually sends a www-authentication request because first I assumed it didn't, as the first answer in this thread suggested
-
@Depronized
Perhaps not helpful/does not apply to you, but please read the "box-outs" in https://doc.qt.io/qt-6/qnetworkaccessmanager.html#authenticationRequired (and maybe https://doc.qt.io/qt-6/qnetworkaccessmanager.html#proxyAuthenticationRequired too) carefully. Any chance it worked first time and is cached? Are you using threads/queued connections? Does youfinished()
signal return theAuthenticationRequiredError
error? Does it matter whether you set username/password or not? Just some things for you to check. -
@JonB I also thought it could be due to cache, so I ran the manager.clearConnectionCache command but that does not help.
I also tried to find a way to force it to provide credentials but I have not found any other way to do so. But that was when I still wasn't sure that the server actually requests this, but since I now know that the server sends the www-authenticate request I know the error is in my applicaton or in qt.
Yes, I get the QNetworkReply::AuthenticationRequiredError. So weird that I get that error and not the authenticationRequired signal.
-