signalR generating different ID for various instances - asp.net

I have created web chat application using signalR. When user is logged in, user can open only 3-4 tabs and each time different connection ID is generated. I want that when we open new tab or refresh the page new connection id should not be generated. I want to use the existing connection id. For that I tried to implement the LocalStorage variable to store the hub connection.
But Problem is i am unable to parse the value of localstorage variable.
Can Anyone give me the solution to my problem or can anyone give me any other solution to the problem?
I have already tried this http://kevgriffin.com/maintaining-signalr-connectionids-across-page-instances/ but it dosen't work for me

If user is authenticated you can store different connectionIds in the HashSet. The key for the HashSet will be the user's name.
http://www.asp.net/signalr/overview/signalr-20/hubs-api/mapping-users-to-connections
You can use this method even if you are not authenticated. In this case you can use SessionId instead of username.
EDITED:
I have found another method. In this article Alex Ford advices to limit your connections to 3.

Related

Signout the existing login if user signedin again in different device in Identity Server 4

If the user is logged in already and trying to login again in a different device or browser then need to logout the existing one
I am using Identity Server 4 in my application.
Which mechanism or steps we can use to achieve this one?
My startup code:
services.AddIdentity<UserIdentity, UserIdentityRole>(options =>
{
// Basic built in validations
})
.AddEntityFrameworkStores<IdServerDbContext>()
.AddDefaultTokenProviders();
Any help would be appreciated
Thanks in advance.
I remember that I was thinking about a similar feature for a website I was developing, that also used Identity Server. If I recall correctly then there isn't anything for this built into Identity Server. But, that doesn't mean you can't code it.
One way would be to keep a integer based log-in counter for the user and increment that each time the user signs in.
Let's say, Bob, signs in on his phone. This is the first time he signs in, ever. Bob's record in the database, has a column that is called SignInCounter and it's now set to '1'.
Now if Bob signs in on his PC, let's say an hour later, we set the SignInCounter to '2'.
Now all you need to do is check whether the claim value for SignInCounter matches the value that's in the database. It can easily be done on a per-request basis, by using middleware. If it doesn't match, show an error page, sign-out the user, what ever fits your case.
I really wouldn't recommend this though. It doesn't scale and also generates +1 database call per request, but hopefully this gives you a general idea how to solve the problem.

Signal R for notifying the single authenticated user

Following is the scenario I want to achieve using signal R.
If sending of email is going on then message will get display
Example: "Sending Email.."
Once email sending is done will show the another message to the same user stating
Example: "Email Sent successfully."
How I will achieve this with Signal R and sending the notification to the one user only.
Read the post which are saying use Connection Id but that will be changing not a fix
I want to do this for the Authorized user only or we can say with static userid.
I see two different approaches to the problem, with two different solutions. Either the server keeps track on which connectionId(s) that belong to a particular user. Or the client lets the server know which connectionId to use.
Option 1: Server knows how to map user to connectionId(s)
Firstly you need to map each user to the connectionId(s) they have. Bear in mind that the user could have several connectionIds as he/she could be logged on in multiple tabs or browsers. On the server it is difficult to distinguish between connections so when the user sends an email, it is easiest to notify on all connections that belong to that user.
There are several ways to keep track of users connectionIds. Here is a good walk through. In your case I think the UserID provider option would be a good fit, as it's easy to implement and sufficient if you only want to send updates to the current user.
Option 2: Client notifies server on connectionId to use
Another approach could be to include the connectionId in the call to the server. That way the server knows which connection to report back to. You can get the connectionId in Javascript (as described in this answer) using:
var c_id = $.connection.hub.id
Hope this helps!

Need help understanding sessions and user profile

I'm currently planning my web application. I was thinking using Session to store user profile (user name, current database, permissions, etc...). Common scenario is where user opens several tabs for different pages.
Now, I need to have an option for user to change the database. Basically needs to choose the database from list, enter a log in information and it's done. Not sure how to handle this. It seems that browser tab where user initiated the change should somehow tell server that he needs a new session here or server has to inform browser that new session is generated?
Basically if user has 4 tabs open and initiates a database change on one tab then he should end up with two sessions? Is this correct or is there a better way to handle this?
As far as I am getting your problem, one thing can be done that whenever the user selects a new database and enters the log-in information then on selecting the new database, you need to clear out the old session details for the database and on entering the new session for login information, override the login details.
Also, store the old information into another session and whenever a conflict occurs you can navigate to the error page saying that "Login information and databse have been changed" or any custom message.
Only work around needs to be thought of for this scenario as in same browser the session value will be same throughout.

Support of two authentication modes

Currently I have a site with usual authentication mode: login/password. The new authentication mode must be added (some number which will be verified by another system, an answer will be returned and a user will be logged in or not). So I have to store this number in my DB in addition to login/password data.
User can upgrade his/her account to use the new authentication mode, but after this the old one will not be available anymore. Only the new one will be used for the registration of new accounts.
Could you please suggest the best way to handle this situation? The simplest way in my opinion it's just save this new code in some additional property and check if this property empty or not (or add some enumeration LoginType with corresponding values).
UPDATE:
I'm wondering whether can i just use the provided above way to solve the problem or it will be a bad workaround? Also (as the second approach) I thinking about removing the current record (user account) and adding a new one with the new login name on account upgrade (when user changes the way to log in). Now I use DefaultMembershipProvider.
Thank you in advance for your answers.
Question related to ASP.NET (Web forms), not ASP.NET MVC.
Maxim

how can i use cookies information in flex3

can I use cookie information in flex? Suppose a user logs in first time in login form (designed in flex) and when he logs in again I need to show users name in dropdown from cookies, is it possible in flex? Is there any way I can do please suggest me Thanks in Advance.
You can create a SharedObject to store session data on a users computer.
var so:SharedObject = SharedObject.getLocal("savedData");
If a shared object by the name you specified doesn't exist, one will be created.
Check out the Live Docs for more info:
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/net/SharedObject.html
You should use cookies for uses identification on the server (a session id) just like you would with any other web app. Then have a service on your back-end like getUserInfo() that returns the information for the currently logged in user. Call that method from Flex (via HTTPService, WebService, RemoteObject, etc) and then you will have the user information on the client.
See a number of SO threads on this issue:
Accessing browser cookies from Flex
How do i store cookies in flex?
Briefly, two options that don't involve going back to the server:
Use Javascript integration to access cookie data, as in this library.
Use SharedObjects ("Flash cookies") to persist local data.

Resources