I have a SignalR Chat that is available to anonymous users. I need a way to map the users so that the connections persist on page reload, and if the user has multiple tabs they should get the message displayed on every tab.
If I use Context.ConnectionId, every page reload creates a new connection. I want to map these connections using Single-user groups.
For logged-in users, I use Context.User.Identity.Name:
Groups.AddAsync(Context.ConnectionId, Context.User.Identity.Name);
Is there a similar way to get an anonymous user's "identity"? From what I have read, session is not supposed to be used in the SignalR hub, but all the information I found is old, so I may be wrong here.
When you reload a page the existing connection will be closed and a new one will be opened. The new connection will have a new connection id and on the server side you will not be able to tell what user initiated it. Depending on your circumstances you could try identifying the user by their ip addresses - i.e. you would store the user ip when the connection is open and then, when a new connection is opened you would check if you have already seen the ip. This may not work however because the same user can actually have different ips and multiple users can have the same ip. Another method would be to send a client side generated identifier in the query string when opening the connection and use that to identify the same user on the server side.
You must implement some mechanism to create unique ID for each user.
I would do something like following
Before user actually connects to Hub i would create a unique ID and
store it in cookie
set the query string for signalR URL with the value of this cookie so
that each call to hub will avail with the user's ID
now even when the user refreshes the page, the old connection would
automatically get removed from the group by signalR and you can
continue adding new connections mapped against the uniquely generated
ID.
Related
With the command prompt it is possible to query the sessions for a server, as shown here (https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/query-session).
With an aspx file you can get a unique sessin ID by using:
System.Web.HttpContext.Current.Session.SessionID
Also, with an aspx file you can get the IP address by using:
Request.UserHostAddress
Is it possible with an aspx file to obtain the session name (e.g. console, rdp-tcp#10) and the corresponding ID number (integer)? I am trying to integrate an old piece of software that records a terminal/server ID number as part of key to record a user's request in a database.
Background:
I have a file with the extension .aspx from around ~2011 that integrates an enterprise database with software delivered by a web browser (software as a service?). This file contains a call to Request["sessionId"] which I don't think returns anything as the key "sessionId" may nolonger be supportted? For the file logic, all I need to know is whether the user has the "console" session type or not. This aspx file is called by a batch file whenever a user presses one of the buttons on the enterprise database.
I built an c# desktop application for attendance
application is working fine when i run it on single machine and all is good..
but now issue is ... my boss want this application should work on LAN and every client can access application from server
but i used static variables which store some values at run time and then i utilized these values in next forms.
here is example..
on login form username is stored in static variable as
public static string username;
username = txtUser.Text.Trim().ToString();
and in next form which is Home Page this user name is used as
lblName.Text = Login.username;
here lblName is name of label and Login name of Login form
when i configure this application on LAN then may be at same time more than one user will be logged in and want to access application then each user should see his/her own username....
e-g abc is user 1 and xyz is user 2
if both are logged in at same time then i want abc to see
lblName.Text = "abc"
and xyz to see
lblName.Text = "xyz"
so what should i do to handle this job?
i need not to mix up any information between users...
i used c# windows app.
thanks in advance.
I assume you use static variables on server-side, right?
So, your issue happens because you use static variables.
Static variables are consistent in your class during all period of application is running. And that fields are shared between ALL processes which can use that class. Static variables was designed for this.
As a possible quick solution you can generate something like unique session Id. During client logs in to your system - you need just generate that id on client's side, send it with login information to server. In case of successful authentication server will store id in cache a key. And value of the cache can be object with user's name and other needs.
Every request to your server should be followed with that id. So your server will always know which user it is.
If you need more detailed answer please provide more information about app network structure and it's modules.
I'm writing a pretty big web system with asp.net MVC, which involves sending data in Real Time to numerous users, based on what they are subscribed to.
My team and I decided to use SignalR for that, and I am in charge of implementing it in the system.
In our case, that a user picks a group to join, and then picks 1 Thing to work on.
For that, I'm saving all the users in a DB. I'll be using the SignalR Groups to handle the first category, and when I need to push a message to a specific user (for the other thing hes picking) I'll just get his ConnectionID from the DB.
Here's the problem - every time the page is refreshed (for instance, when the user picks a group to join) he gets a new connectionID. And now he won't see anything that's pushed to him.
I saw that in the SignalR beta, and on version 2 (I only have 1.1.1 on the computer I'm working on) you can make your own IUserIdProvider (IUserIdPrefixGenerator in the beta), or IUserConnectionIdFactory etc. So I can give him the I'd I want him to have, but I don't seem to have any of those in my version of SignalR.
There are many ways to solve this, but perhaps one of the simplest ways is to associate the new connection id with the user (maybe they still have the other connection open in a different tab). This can be done using any combination of IP Address, User-Agent, Headers, or location. Another good candidate for this is either to use sessions, or just a simple identifier cookie (which is more or less what a session would do anyway).
I'll often use GUIDs for this and then create a table in the database when a new identifier cookie is created. Every time the user "refreshes" or opens a new tab, the cookie can be read in JS and sent with the hub.connect(). Then, you can create an association between the new connection id and the existing identifier cookie.
I'd highly recommend figuring out a different way to maintain your users' persisted connections. Typically, I keep all of my users connection ids stored in a concurrent dictionary to allow for thread-safe access to the collection. I remove the users from the dictionary whenever a disconnection event occurs and I add them whenever a connection event occurs.
SignalR will manage your users' connections for you. For you to do it in the database and fall out of sync with SignalR circumvents a lot of the mechanics that make it work correctly in the first place.
private readonly static Lazy<App> _instance = new Lazy<App>(
() => new App(GlobalHost.ConnectionManager.GetHubContext<AppHub>().Clients));
private readonly ConcurrentDictionary<string, User> _users = new ConcurrentDictionary<string, User>(StringComparer.OrdinalIgnoreCase);
private IHubConnectionContext Clients { get; set; }
public App(IHubConnectionContext clients)
{
Clients = clients;
}
In asp.net 4.0 c#, i have two databases on different servers 1.Sql server 2.Oracle
I want to develop a web application in which the users will login with their id password and then
perform the allowed tasks.
I want to know that how i can make a single connection string for multiple users?
I dont want to make connection string on every page.
I was looking for this for a whole day but could not found any solution.
currently i am not using web.config file but i have a class where i have made a connection string and passed the textbox values to it
and the storing them in static fields and suppling them to other classes , but i dont think it is good approach
i think most of people are not getting what i really want to ask
kindly let me know if you want any confirmation.
for example there are 40 users each having a different id,password (no of users can be increased or decreased) there are two approaches to have a connection string
1)make a connection string in a class
string conn=#"server=MYSERVER;database=mydb;user id="+userid+";password="+password);
but how the userid,password fields can be accessed in web.config? here userid=textbox1.text,,password=textbox2.text;
2)make a connection string in a web.config file
so how?
It is very common for all users to use the same connection string to talk to the database server for a web application, and for the application to control what functions the users can and can't do based on their userid and password and security you build into you application.
In this type of setup, the user's id and password are separate (and different) from the userid and password for the database server.
If you must use a separate userid and password for each users db connection, then you wouldn't store it in a connection string in your config file, but would instead build it dynamically based on the user logging in.
Like this for example:
var userid = "testuser"; //these would come from your login page, not hardcoded
var password = "letmein";
var sqlConn = new SQLConnection("server=MYSERVER;database=mydb;user id="+userid+";password="+password);
I have count the number of online user to the web server. I have created the username & assign the password to individual computer then using this user name user can access the main page & after login user I have add the username, session ID, IP address in datatable. If i user already connected from another computer then same user access the information from another computer then user first disconnect the already connected from current computer using kill the session from sessionID . then another computer functionality automatically disabled to user then user can start the new session for current computer.
How to kill the session from Unique sessionID & disabled the all functionality of the another computer user?
i would try it this way :
On login generate a random string, write it into a session variable and store it in database, then in ur page compare this values against each other to verify user access. this way a user can be logged on only once.
you can use this code:
Session.Remove("keyOfSession");
For example you could have a property object in your ApplicationInstance, such as a Dictionary :
Key would be the userId
Value would be a list of KeyValuePair where
KVP Key would be user IP
KVP Value would be a WeakReference to a session object
At the end of a Session_Start, you insert an item in your Dictionary=>List for the corresponding user and IP holding a WeakReference to the Session object.
Then, you also End() all other not null, weak referenced, Session objects for this user and other IPs (be aware that some users access the internet through dynamic proxies that may change between requests)