PDA

View Full Version : How to use SSPI (NTLM)?



tonka3000
13th December 2012, 11:32
Hy@everybody,

in the last few days I play around with QWebKit (and network). I only wanna do a simple thing: I want do dowload only a simple webpage. My problem is the proxy in our comapny, it requires and authentication. All works well. But I want to setup my program in the way the the user should not setup the proxy himself. I have searched the web for it and find out that (in Windows) the key-technique is SPPI (NTLM) => and here is my problem:How can I tell Qt to use SSPI instead of "handmade" proxy username and password. I only can get the hostname and the port, but the username and password has to be sent from windows (secutiry) itself (using SSPI).

Here is my Testcode (only.cpp):

#include "httpui.h"

HttpUi::HttpUi(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);

nam = new QNetworkAccessManager(this);
HideWidgets();
connect(ui.getButton,SIGNAL(clicked()),this,SLOT(a ctivateGetWidgets()));
connect(ui.submitButton,SIGNAL(clicked()),this,SLO T(DoHttpGet()));
connect(ui.resetButton,SIGNAL(clicked()),this,SLOT (clearWidgets()));
connect(nam,SIGNAL(finished(QNetworkReply*)),this, SLOT(finished(QNetworkReply*)));
connect(ui.postButton,SIGNAL(clicked()),this,SLOT( activatePostWidgets()));

connect(nam, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)), this, SLOT(onProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
}

HttpUi::~HttpUi()
{

}

void HttpUi::onProxyAuthenticationRequired(const QNetworkProxy &prox, QAuthenticator *auth)
{
auth->setUser("myuser");
auth->setPassword("mypassword");
}

void HttpUi::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui.retranslateUi(this);
break;
default:
break;
}
}

void HttpUi::activateGetWidgets()
{
ui.urlLabel->setHidden(false);
ui.urlLine->setHidden(false);
ui.submitButton->setHidden(false);
ui.textBrowser->setHidden(false);
ui.responseTitleLabel->setHidden(false);
ui.getButton->setHidden(true);
ui.postButton->setHidden(true);

}

void HttpUi::activatePostWidgets()
{
ui.dataLabel->setHidden(false);
ui.dataLine->setHidden(false);
activateGetWidgets();

}



void HttpUi::finished(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError)
{
ui.textBrowser->setText(reply->readAll());

}
else
{
ui.textBrowser->setText(reply->errorString());
}
}

void HttpUi::DoHttpGet()
{
ui.resetButton->setHidden(false);
QString url = ui.urlLine->text();
QString data = ui.dataLine->text();
QByteArray postData;
postData.append(data.toAscii());
if(postData.isEmpty() == true)
{
nam->get(QNetworkRequest(QUrl(url)));
}
else
{
nam->post(QNetworkRequest(QUrl(url)),postData);
}

}

void HttpUi::HideWidgets()
{
ui.urlLabel->setHidden(true);
ui.urlLine->setHidden(true);
ui.dataLabel->setHidden(true);
ui.dataLine->setHidden(true);
ui.submitButton->setHidden(true);
ui.responseTitleLabel->setHidden(true);
ui.textBrowser->setHidden(true);
ui.resetButton->setHidden(true);

}

void HttpUi::clearWidgets()
{
ui.urlLabel->setHidden(true);
ui.urlLine->setHidden(true);
ui.dataLabel->setHidden(true);
ui.dataLine->setHidden(true);
ui.submitButton->setHidden(true);
ui.responseTitleLabel->setHidden(true);
ui.textBrowser->setHidden(true);
ui.resetButton->setHidden(true);
ui.urlLine->clear();
ui.textBrowser->clear();
ui.dataLine->clear();
ui.getButton->setHidden(false);
ui.postButton->setHidden(false);

}
The code is from here (http://www.developer.nokia.com/Community/Wiki/How_to_do_HTTP_Get_and_Post_using_Qt). I only add the connect for proxyAuthenticationRequired-SIGNAL => so if a password is required i can set it.
I can't get it work over SPPI because I don't know how to use SSPI in Qt!

So my question is: How can I use SSPI-Authentication instead of setting it myself?

Qt: 4.7.3
OS: Windows 7 x64

Greetings
Tonka