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 4 views 4 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 How to Stand Out as a Microsoft Student Ambassador: Perks, Process, and More… September 9, 2024 Optimizing a Terabyte-Scale Azure SQL Database September 7, 2024 Installation/Validation of extension-based hybrid worker September 7, 2024 New Surface Pro & Surface Laptop September 7, 2024 What's new in Microsoft Teams (free) | Aug 2024 September 6, 2024 Azure Durable Functions: FaaS for Stateful Logic and Complex Workflows September 6, 2024 Leave a Comment Cancel Reply Save my name, email, and website in this browser for the next time I comment.