Home > Mobile >  Python Flask on IIS: How to get username of person logged in on IIS?
Python Flask on IIS: How to get username of person logged in on IIS?

Time:03-04

I have looked at several posts like How do I get the username of the current user in a Flask app on an IIS server using Windows Authentication? which are either several years old or don't have an answer

I have an IIS server with Python Flask. IIS has windows authentication enabled and everything else disabled. This is an intranet/internal application. If using ASP, it's easy enough to get the username. I have a page that does this called user.asp (this comes into play in a bit)

What is the easiest way for me to get the username of the current user using Python Flask?

I tried using requests to access the ASP page but I get an error: HTTP Error 401.2 - Unauthorized

def get_user():
    r = requests.get('https://mywebsite/users/api/user.asp')
    return r.content

I've also tried

from flask import request
...
@app.route('/list', methods=['GET'])
def get_default():
    return str(request.environ)

This doesn't show anything containing my username

Tried Enable autologin into flask app using active directory and more specifically request.environ.get('REMOTE_USER') and get None

Any suggestions?

CodePudding user response:

It actually was answered here, he got the name of the user by: request.headers.get('X-Remote-User')

CodePudding user response:

After reading several articles and testing out several more methods, this was the one that proved successful in the end

https://medium.com/@nerdijoe/how-to-deploy-flask-on-iis-with-windows-authentication-733839d657b7

  • Related