California Consumer Privacy Act (CCPA) Opt-Out Icon by info.odysseyx@gmail.com August 29, 2024 written by info.odysseyx@gmail.com August 29, 2024 0 comment 21 views 21 problem : Deploying a Python Flask application to IIS can be a smooth process, but sometimes you will run into issues that require careful troubleshooting. One such issue is that Flask applications fail to retrieve the Windows Authentication user ID when using HttpPlatformHandler. While we were able to retrieve the user details successfully using WFastCGI, we were not able to do so using HttpPlatformHandler. Let’s take a look at how to retrieve the user details in such a scenario. Some pointers: Moving from WFastCGI to HttpPlateFormHandlers: WFastCGI is no longer maintained, see here. Configuring a Python Web App for IIS – Visual Studio (Windows) | Microsoft Learn Configuration adjustment: The key steps are to enable: ForwardWindowsAuthToken In the options Http platform handler Configuration. This setting passes the Windows Authentication token to your application so that you can access and process it within your code. Code implementation: After adjusting the configuration, you can update your Flask application code to retrieve the Windows Authentication user ID. The following code snippet shows how this can be done. import flask from flask, request, render_template Import os import win32api Import win32security Create create_app(): app = flask(__name__) @app.route(“https://techcommunity.microsoft.com/”) def hello_world(): s_vars = request.environment user = os.environ.get(‘USERNAME’) handle_str = request.header[‘x-iis-windowsauthtoken’] handle = int(handle_string, 16) win32security.ImpersonateLoggedOnUser(handle) user1 = win32api.GetUserName() win32api.CloseHandle(handle) f returns “Hello World!: {user1}”. Return the app This code snippet demonstrates how to impersonate a logged in user and retrieve their username using the win32api and win32security modules. The key element here is the x-iis-windowsauthtoken header, which contains the Windows authentication token passed in from the HttpPlatformHandler. Dependency Guarantee: Make sure you have the pywin32 package installed, as it provides the functionality needed to interact with the Windows API from within the Python environment. For more information, see the following resources: Source link Share 0 FacebookTwitterPinterestEmail info.odysseyx@gmail.com previous post “Exciting Product Management Job Openings at Avaari in Ahmedabad: Apply Now for Lucrative Roles” next post Application Initialization in IIS You may also like Galaxy S25 Ultra how finally removed me from my iPhone addiction March 20, 2025 Low Earth orbital networks are pressing for the innovation of geostationary giants March 19, 2025 AI Chattbots ‘Zero-Jnan’ may be easy victims for hackers March 18, 2025 Sevatton Dual Screen turns ad-on laptops into triple display March 17, 2025 Microprocessor market problems with market conditions and tariffs March 17, 2025 Believe Hyp about Quantum Protection: Report March 11, 2025 Leave a Comment Cancel Reply Save my name, email, and website in this browser for the next time I comment.