What are some methods to prevent cookies from being overridden?

Just beginning my journey in web development. Currently utilizing asp.net Web API and Angular with token authentication. Every time a user logs in, I set the token in a cookie and send it with each request. Everything has been running smoothly so far, but I encountered an issue when a user logged into two different accounts on separate tabs within the same browser. The cookie ended up being set to the last logged-in account, causing the tokens for both accounts to be the same. How can I differentiate between them?

Answer №1

Ensure the current user's username is included in a personalized header for each page request. Additionally, implement an Angular interceptor to send the username back in a custom header with every client-server call.

When handling incoming requests on the server side, validate the username stored in the session against the one provided in the request header. If they do not match, it indicates that the user may have logged into a different account in a separate tab. In such cases, return an appropriate error response.

Note: For scenarios where you want to isolate sessions and avoid sharing cookies (e.g., during QA testing), consider opening new tabs in incognito mode or using a different browser altogether.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What method does asp.net utilize to generate the names of compiled dll files from .cs files within a project?

During my code review of a web project, I realized that I only have three .dll files for static analysis, but the project includes 20 different .cs files. This discrepancy has raised questions about whether I am missing any compiled dll's. All of the ...

An issue arises when trying to loop using a while loop within an HTML structure

I have a small project with a predefined format, where I need to select a value from a dropdown menu and use that value to fetch data from a database and display it in HTML. While I am able to retrieve the data from the database based on the selected value ...

Angular JS - the culprit behind app crashes in Safari and IE

I'm encountering an issue with my Angular JS app. There is some code that counts the characters from an array, but for some reason it is causing the app to break and stop working in Safari and IE. I've tried to figure out what's wrong, but c ...

Look through the contents of each child within a div to locate specific text, then conceal any that do not include it

I want to dynamically hide divs that do not contain the text I specify. Here is the code I have tried: var $searchBox = $('#search-weeazer'); $searchBox.on('input', function() { var scope = this; var $userDivs = $('.infor ...

Having difficulty rendering Twitter Bootstrap glyphicons correctly

While following an Angular.js Todo application video tutorial, I ran into a snag trying to include Twitter Bootstrap 3 glyphicons in the index.html file. The icons were displaying as unfamiliar images in both Chrome and Firefox. Here is how I am adding th ...

When I use my loop to generate Google Map markers, the positioning is not accurate and the markers do not appear on the map. However, manually inputting the positions

There seems to be an issue with displaying Google map markers based on objects in the markers array after looping through it. I noticed that when creating a second component and setting the position manually, the marker appears correctly. However, upon ins ...

What's the best way to ensure an endless supply of copied elements with identical classes to the clipboard?

Here is a snippet of my HTML code: <Div Class='POSTS'> <Div Class='POST'> <Div Class='CONTENT'>Text1</Div> </Div> <Div Class='POST'> <Div Class=&apos ...

Error message displayed in Ajax jQuery shows '[object Object]'

There seems to be an issue with this code on certain computers. I have a basic AJAX call set up to display a list of batches in a select tag. function show_batch(class_id,batch_id){ if(class_id){ $.ajax({ url:root_path+"module/fee_ ...

Steps for changing tabs in Ionic depending on a specific condition

I have a mobile app built with the Ionic Framework that features 4 tabs, but I want to limit the visibility of only three tabs at a time. The first two tabs (Tab1 and Tab2) are always visible. However, the third tab should display either Tab 3a or Tab 3b ...

The if-else statement is providing a misleading outcome

While working on my map using leaflet, I decided to implement a dynamic color concept based on input data. However, despite comparing 3 sets of data to ensure accuracy, some parts of the color scheme are displaying incorrect results. Below is the snippet o ...

Deleting outdated files in a temporary uploads directory - NodeJS best practices

My process for removing old files from a tmp upload directory involves the code below: fs.readdir( dirPath, function( err, files ) { if ( err ) return console.log( err ); if (files.length > 0) { files.forEach(function( file ) { ...

Issues with CSS Modules not applying styles in next.js 13 version

Employing next.js 13.1.1 along with /app Previously, I had been handling all of my styles using a global.css, however, I am now attempting to transition them into CSS Modules. Within my root layout.js, there is a Header component that is imported from ./ ...

Running nodejs scripts within my HTML and JavaScript code

Using express, I send an HTML file to incoming GET requests: app.get('/', function(req, res) { res.sendFile(path.join(__dirname + '/public/index.html')); }); Within that HTML file, I included another JavaScript file, script.js, us ...

Testing an ExpressJS route and their corresponding controller individually: a step-by-step guide

I have set up an Express route in my application using the following code snippet (where app represents my Express app): module.exports = function(app) { var controller = require('../../app/controllers/experiment-schema'); app.route('/a ...

What is the most effective method for populating an array with all images from a particular directory?

I recently installed a pdf viewer plugin on my website (check it out here: ), and I have a question regarding its functionality. I am looking to include approximately 60 - 70 pages in this flip book, but I am unsure of how to proceed. I have attempted var ...

Different from SimplyScroll but with added functionalities

Searching for a replacement for the now deprecated SimplyScroll with specific features. I am in need of a continuous, automatic carousel of boxes/images that halts when hovering over with the mouse (a feature SimplyScroll possesses), and allows movement ...

Adapting designs within an Embedded Frame - JQuery

I am dealing with a dialog type popup that appears when a button is clicked by the user. Inside this popup, there is a form that needs to be submitted in order to change the width and height of the popup using the following code: $(document).ready(functio ...

How can we extract validation data from a function using Ajax and then transfer that information to another Ajax query?

I have an AJAX function that validates a textbox on my page. After validating, I need to perform an action with that value (search in the database and display results in the textbox). However, I also need the validation function for another separate functi ...

I am experiencing some unwanted movement of divs when I hide one element and show another. Is there a way to prevent this from happening

My webpage features a dynamic div that transforms into another div upon clicking a button. Although both divs share similar properties, clicking the button causes some elements to shift unexpectedly. Strangely enough, when the height of the replacing div i ...

What is the process for extracting components from a JSON file using an observable in Angular?

Take a look at this snippet of code: response: any; fetchData(url: any) { this.response = this.http.get(url); } ngOnInit(): void { fetchData("url.com/data.json"); console.log(this.response) } When I check the console, I see Obser ...