Prevent log-in for an Already logged-in user C# - c#

I have a desktop C# application, several clients are connected into SQL server. Each user has an ID and Password. what do i want is to prevent an already logged-in user to log-in from another computer.
I have implemented it by updating a Field in the database 'UserLoggedIn'=true at the login, and when the user logs out the Field Updated into False. But this solution is not optimal, in which if the system crashed or the computer shutdown unexpectedly, the value will stay 'UserLoggedIn'=True in the database and this user will never can log-in again to the system.
What is the optimal solution for that, and prevent an already logged-in user from logging-in from another computer?

Instead of true, store a timestamp in the database. Make sure that you update it periodically while the user is logged in.
When they log in, allow them if it's not too new. That is: if you've seen them from another PC within the last (say) 30 seconds, then consider that session still live, and reject the new login.
This way, if their other PC crashes, the timestamp will not be updated, and will eventually expire.
The update frequency should be about twice the expiry timeout. For example, if you want a session expiry of 1 minute, you should update the timestamp every 30 seconds.

I would suggest that you check if the user has already established a connection to the database using the following query
SELECT loginame as LoginName,program_name
FROM sys.sysprocesses
WHERE db_name(dbid)='my_database' and loginame='test_user'
If the number of records returned is greater than zero then you can assume that a user has already connected to the database from your application.
If you look at the connection string property in MSDN, you will find the following properties of interest. These 2 properties can be added to your existing connection string to further improve the filters that can be defined in the query above
Application Name - The name of the application, or '.Net SqlClient Data Provider' if no application name is provided. (=program_name in the table above)
Workstation ID - The name of the workstation connecting to SQL Server. (=hostname in the table above )
I believe that this solution will work irrespective of crashes on the client side and does not require that you save the state in any table.

Related

Can't store users in the default MVC application

I had some problems with using the authorization before so I got a brand new everything - new computer, new OS, fresh installation of VS, new app and DB in a new resource group on the Azure. The whole shabang.
I can confirm that I can log in to the Azure DB as the screenshots below show.
I can see the databases, tables, users etc.
The problem is that, although it works locally (using the default connection string provided automagically for me), it doesn't perform very well in the Azure (although I'm using the publish file from there). It said something about the file not being found and according to this answer, I needed to change the connection string.
After I've altered it, I get the following error. Please note that the firewall is open and that I can access the DB when I run the code of my applications. I feel that there's something that goes wrong when the authentication part is automatically configured. I'm out of ideas on how to trouble-shoot it, though.
[SqlException (0x80131904): Login failed for user 'Chamster'.
This session has been assigned a tracing ID of '09121235-87f3-4a92-a371-50bc475306ca'. Provide this tracing ID to customer support when you need assistance.]
The connection string I'm using is this.
Server=tcp:f8goq0bvq7.database.windows.net,1433;
Database=Squicker;
User ID=Chamster#f8goq0bvq7;
Password=Abc123();
Encrypt=True;
TrustServerCertificate=False;
Connection Timeout=10;
This issue's bothered me for a while and I'll be bounting it in two days. Any suggestion's warmly appreciated.
I believe I've managed to resolve this weird issue. It appears that the user I'm using, despite being admin with all bells and whistles isn't recognized as admin when used in the connection string and trying to create the tables (which is the case at the first registration).
My solution was to create two logins - one with db_owner role and one with db_datareader and db_datawriter. First, I've used the elevated user in my connection string and registered a single user. That created the tables in the database as shown below.
Then, while able to continue as admin, I realized that we should try the demoted user and tada!, it worked perfectly. Once the tables were there, the whole shabeling behaved as expected.
To be perfectly sure, I dropped the tables from the database and there it was - the same issues as before. When I changed to the elevated user, the tables were restored allowing me to get back to the demoted one.
I also tried dropping the tables, confirming the issues to re-appear and then creating the tables manually. That works too! So basically,the only gotcha that caused it all was the original admin who's not treated as admin.
It might have to do with the fact that my Azure account's getting a bit old, LiveID used there is ancient and that didn't have an updated version of DB in Azure (the pull-up to v12 was carried out the 18th of December, so it's possible that it also was a requirement to get it working). I'm too tired and lazy to check that out and I realize that I've no idea how to get an "old" type of account. Besides, the issue will decrease and gradually vanish because the old accounts get upgraded eventually.

Tally database synchronization with c# Application

I want to make an application to sync Tally Sales Order and Sales Invoice from tally to our SQL Database. Currently for testing purpose I am using Tally ERP 9 Educational Version.
I have created some sales orders in tally and need all order with their details from tally using tally ODBC Sql query
uptil as per my research, I am getting few sales order details like Voucher Number,Order Date,'Voucher Type'.. etc. from tally ODBC table CompanyVoucher. but few details came empty, although related data exist in tally order. like Reference , Party Name ... etc.
Also, I am unable to find Tally ODBC table to get few other sales order related data like item name, item number , item quantity, rate and order total, order no etc.
Can anybody suggest SQL Query or Tally ODBC Table from where i can find these order related data. Also I am not sure, if we can not access these details due to Educational Version, and any limitations on Educational Version on access of these details.
so please suggest me on this.
Synchronisation is the process of replicating data between two or more computers using Tally.ERP 9 in a Client – Server environment. Data is transferred from the client to the server and vice versa. Tally.ERP 9 offers control over the frequency of Synchronisation i.e., data can be replicated after each transaction or updated at specific intervals.
Configure Server for Synchronization
To configure the Server Company for Synchronization, follow the steps given below:
Go to Gateway of Tally > F12: Configure > Advanced Configuration
In the Advanced Configuration screen,
Under Client/Server Configuration section,
Set the option Tally is acting as to Server, to run Tally.ERP 9 as Sync Server
Specify the required port number (e.g. 9009) in the Port field....
Tab down to Tally Sync Configuration section:
Set the option Ignore Clients modified Voucher Type Masters to No, so that modifications made to Voucher Type Masters on the Client are updated on the Server
Set the option Enable Sync Logging to Yes, to generate a Log file which contains synchronized vouchers and is saved in the Tally folder.
Set the option Truncate previous log before Syncing should be set to Yes, to allow the previous log file to be overwritten by the current log file.
Go to end and press Enter to save the details.
For the above changes to take effect, Tally.ERP 9 will prompt you to restart the application as shown:
Press Enter or select Yes to restart Tally.ERP 9 on the Server computer.
Tally.ERP 9 will restart and in the Information Panel you can see that it is configured as the Sync Server as shown below.
Detailed reference here.
If the datas are present and you cannot access the sync then there
must be restriction in your version

How to create an automated column in SQL Server?

IDE: Visual Studio, C# .net 4.0, Winforms application
Is there any way in SQL Server to implement a column in a table which can set itself automatically to 0 when it has not received any ACK (acknowledgement) signal from the application side?
The requirement is I want to keep a column which can keep track that Is application is open or it has been closed?
One way to implement is using the OnClose() event, i.e. on Close() I can change it's value to 0. but the problem is suppose application got hanged for some reason or Power is gone than the value in database will not be updated to zero.
So I want to create an automated column which can track this situation and make itself zero when the application is not sending any request or idle means closed.
please suggest how to handle this.
You can't do that. The only thing you can do is to save GETDATE() in a column in a table as the last activity time of the application and invoke the stored procedure from a high-priority thread every 10 seconds for example.
When you want to know if the application is alive or not, just check this value, if more than 10 seconds is passed since then, you app is gone.
UPDATE:
A more precise but complex approach would be to listen on a socket inside your application and then whenever you want to know if the application is alive, send a request from your sql script to PING the application. You should use CLR programming for this approach, but I think the first one will be practically enough.
Considering it will be a multiple instance scenario where multiple instances of the application can point to same database. You can try the following:
Create a separate table to maintain sessions. This table would contain three columns 1)Machine name or IP 2) Session Id (GUID) and 3) TimeStamp.
Whenever application starts create a new session id and make an entry into this table it means new session is started.
Keep on updating timestamp on every request based on session id.
Have a timeout configured somewhere in web.config or database which will come in handy later.
Now when application is exiting gracefully then delete the row from the table.
Now if application crashes the row won't be delete so next time when application starts you can check if the row already exists. Use machine name or IP to query to the table.
Use the timeout setting configured in (3) above to determine since how long the application is idle.

Auto Log Off in SQL Server 2008 R2

In my database on my SQL Server I have a table with users. This table have a column named logged_on which is a bit, either true or false. When someone log on the column will be true, else false. If something ever happens when someone is using my wpf-application, like the power disappears and the application couldn't log out the user this column will be true and the user will not be able to login again, because the application will tell this user that he is already logged on. Is there something i could do to avoid this situation? On the server-side or in the application?
Any sugestions are appreciated :)
I think you'll need a "LastActive" date-time stamp. Then run a scheduled job that says "if you've been inactive for X minutes, change the state of the logged_on flag".
Are you trying to insure that only one login can be active at the same time? Is that the end game?
Sounds like you need to change the approach a little, perhaps tracking last interaction date/time (every time the WPF app calls SQL) and enforcing a session timeout limit instead. You could use that DateTime to automatically log out anyone that hasn't interacted with the server in several minutes by checking it in addition to the *logged_on* bit field (always switching *logged_on* to false if the DateTime is too stale).
Is there an important reason you can't just throw away the old login in situations where the user logs in again without logging out first?

How know who is connected to the application in WPF?

HI All,
i wish begin to work about a feature new for me ,i'n my project i have a database( with SqlServer) and an application (developing in WPF) the purpose of this application is to manage/handle about Accountancy and other Departement,so i created a Passwords and Roles(for Users) to access in the application ,now i will create a system to knows how many Usera are connected to the Application(via Database )and show that in a listBox(for example like a normal Chat Application i wish knows who is online and offline) but i don't know where i need begin to work out this feature ,i'm strange to this feature so if you have any suggestion,link and else i will appreciate so much your help.
Thanks so much.
P.S. if my question is not clear i will rewrite it...however sorry in anticipate
Without knowing the details of your application, I would suggest setting a timestamp field in the database when they log in, and then set it again every X minutes while they are logged in. This has two advantages over just setting a flag:
1) If the application crashes, or the user's machine crashes, the flag will not be reset, and you will have no way of knowing that they are not still logged in.
2) The timestamp will tell you the last time each user was logged in, even if they are not logged in currently.
Maybe in the database itself? When the user logs in, you can set a flag for each user "LoggedInd = true", and when they log out "LoggedIn = false". And do a query on that flag "SELECT * FROM Users WHERE LoggedIn = true"

Categories

Resources