Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing the Windows Authentication context from IIS to Python using FastCGI

I've successfully setup a sample Flask app on Windows / IIS 10.0 using wfastcgi with Python 3.6 running under a Windows domain account.

Now I'm trying to pass the IIS Windows Authentication user information to my Flask app. I've enabled only Windows Authentication in IIS and my browser authenticates successfully.

How do I find out which user is accessing the site in WSGI? I've checked the environment variables and the HTTP headers without luck.

PHP seems to have a fastcgi.impersonate-Option, but there seems to be no pendant for Python.

like image 346
Enno Richter Avatar asked Mar 31 '18 22:03

Enno Richter


People also ask

How does Windows authentication work in IIS?

Authentication: The client generates and hashes a response and sends it to the IIS server. The server receives the challenge-hashed response and compares it to what it knows to be the appropriate response. If the received response matches the expected response, the user is successfully authenticated to the server.

How do I configure IIS authentication in Windows Server 2016?

In the Web Server (IIS) pane, scroll to the Role Services section, and then click Add Role Services. On the Select Role Services page of the Add Role Services Wizard, select Windows Authentication, and then click Next. On the Confirm Installation Selections page, click Install. On the Results page, click Close.


1 Answers

You mentioned that you've checked the environment variables and the HTTP headers. If you checked the environment variables with os.environ.get['REMOTE-USER'] then you should receive an empty string because your Python instance is running locally on the server and is not remote. And unless you use something like ISAPI rewrite, IIS won't write the REMOTE-USER to the headers either.

The easiest solution is to check the environment variables that IIS explicitly passes to Flask:

from Flask import request

username = request.environ('REMOTE_USER')
like image 96
susodapop Avatar answered Sep 29 '22 23:09

susodapop