


How to Redirect Between Domains and Set Cookies or Headers?
How to redirect from one domain to another and set cookies or headers for the other domain?
As described here you cannot redirect to another domain with custom headers set, no matter what language or framework you use. A redirection in the HTTP protocol is basically a header (i.e., Location) associated with the response, and it doesn't allow for any headers to the target location to be added. When you add the Authorization header in your example, you basically set that header for the response which is instructing the browser to redirect, not for the redirect itself. In other words, you are sending that header back to the client.
As for the HTTP cookies, the browser stores the cookies sent by the server with the response (using the Set-Cookie header), and later sends the cookies with requests made to the same server inside a Cookie HTTP header. As per the documentation:
The Set-Cookie HTTP response header is used to send a cookie from the
server to the user agent, so that the user agent can send it back
to the server later. To send multiple cookies, multiple Set-Cookie
headers should be sent in the same response.
Hence, if this was a redirection from one app (with sub-domain, e.g., abc.example.test) to another (with sub-domain, e.g., xyz.example.test) that both have the same (parent) domain (and the domain flag was set to example.test when creating the cookies), cookies would be successfully shared between the two apps (as if domain is specified, then subdomains are always included). The browser will make a cookie available to the given domain including any sub-domains, no matter which protocol (HTTP/HTTPS) or port is used. You can limit a cookie's availability using the domain and path flags, as well as restrict access to the cookie with secure and httpOnly flags (see here and here, as well as Starlette documentation). If the httpOnly flag isn't set, a potential attacker can read and modify the information through JavaScript (JS), whereas a cookie with the httpOnly attribute is only sent to the server, and is inaccessible to JS on client side.
However, you cannot set cookies for a different domain. If this was permitted, it would present an enormous security flaw. Hence, since you are "trying to redirect the user from one application (domain) to another with some cookie set,...*"", it wouldn't work, as the cookie will only be sent with requests made to the same domain.
Solution 1
A solution, as described here, is to have domain (app) A redirecting the user to domain (app) B, with the access-token passed in the URL as a query parameter. Domain B would then read the token and set its own cookie, so that the browser will store and send that cookie with every subsequent request to domain B.
Please note that you should consider using a secure (HTTPS) communication, so that the token is transferred encrypted, as well as setting the secure flag when creating the cookie. Also, note that having the token in the query string poses a serious security risk, as sensitive data should never be passed in the query string. This is because the query string, which is part of the URL, appears in the address bar of the browser; thus, allowing the user to see and bookmark the URL with the token in it (meaning that it is saved on the disk). Also, the URL will make it to the browsing history, which means it will be written to the disk anyway and appear in the History tab (press Ctrl H to see the browser's history). Both the above would allow attackers (and people you share the computer/mobile device with) to steal such sensitive data. Additionally, many browser plugins/extensions track users' browsing activity—every URL you visit is sent to their servers for analysis, in order to detect malicious websites and warn you beforehand. Hence, you should take all the above into consideration before using the approach below (for related posts on this subject, see here, here and here).
To prevent displaying the URL in the address bar, the approach below uses a redirection within domain B as well. Once domain B receives the request to the /submit route with the token as a query parameter, domain B responds with a redirection to a bare URL with no tokens in it (i.e., its home page). Because of this redirection, the URL with the token in it wouldn't end up in the browsing history. Although this provides some protection against certain attacks described earlier, it doesn't mean that browser extensions, etc., won't still be able to capture the URL with the token in it.
If you are testing this on localhost, you need to give application B a different domain name; otherwise, as mentioned earlier, cookies will be shared between applications having the same domain, and hence, you would end up receiving the cookies set for domain A, and couldn't tell if the approach is working at all. To do that, you have to edit the /etc/hosts file (on Windows this is located in C:WindowsSystem32driversetc) and assign a hostname to 127.0.0.1. For example:
127.0.0.1 example.test
You shouldn't add the scheme or port to the domain, as well as shouldn't use common extensions, such as .com, .net, etc., otherwise it may conflict with accessing other websites on the Internet.
Once you access domain A below, you will need to click on the submit button to perform a POST request to the /submit route to start the redirection. The only reason for the POST request is because you are using it in your example and I am assuming you have to post some form-data. Otherwise, you could use a GET request as well. In app B, when performing a RedirectResponse from a POST route (i.e., /submit) to a GET route (i.e., /), the response status code changes to status.HTTP_303_SEE_OTHER, as described here, here and here. App A is listening on port 8000, while app B is listening on port 8001.
Run both apps below, and then access domain A at http://127.0.0.1:8000/.
appA.py
127.0.0.1 example.test
appB.py
<code class="python">from fastapi import FastAPI, FastAPI from fastapi.responses import RedirectResponse, HTMLResponse import uvicorn app = FastAPI() @app.get('/', response_class=HTMLResponse) def home(): return """" <!DOCTYPE html> <html> <body> <h2>Click the "submit" button to be redirected to domain B</h2> <form method="POST" action="/submit"> <input type="submit" value="Submit"> </form> </body> </html> """ @app.post("/submit") def submit(): token = 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3' redirect_url = f'http://example.test:8001/submit?token={token}' response = RedirectResponse(redirect_url) response.set_cookie(key='access-token', value=token, httponly=True) # set cookie for domain A too return response if __name__ == '__main__': uvicorn.run(app, host='0.0.0.0', port=8000)</code>
Solution 2
Another solution would be to use Window.postMessage(), which enables cross-origin communication between Window objects; for example, between a page and a pop-up that it spawned, or between a page and an iframe embedded within it. Examples on how to add event listeners and communicate between the windows can be found here. The steps to follow would be:
Step 1: Add to domain A a hidden iframe to domain B. For example:
<code class="python">from fastapi import FastAPI, Request, status from fastapi.responses import RedirectResponse import uvicorn app = FastAPI() @app.get('/') def home(request: Request): token = request.cookies.get('access-token') print(token) return 'You have been successfully redirected to domain B!' \ f' Your access token ends with: {token[-4:]}' @app.post('/submit') def submit(request: Request, token: str): redirect_url = request.url_for('home') response = RedirectResponse(redirect_url, status_code=status.HTTP_303_SEE_OTHER) response.set_cookie(key='access-token', value=token, httponly=True) return response if __name__ == '__main__': uvicorn.run(app, host='0.0.0.0', port=8001)</code>
Step 2: As soon as you obtain the Authorization token from the
The above is the detailed content of How to Redirect Between Domains and Set Cookies or Headers?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
