How to prevent concurrent login of user from multiple browser

Mar 13 2021 11:29 AM
Hi there,
 
I have been developing an ASP.NET Web application in .NET Framework 4.7.2.I need to incorporate prevention of concurrent login of same user from multiple browsers or from multple devices.I considered to use Session in SQL Server mode for the said purpose.Currently I am facing the following error :-"failed to login to session state sql server for user" after installing aspnet_regsql.exe ,InstallCommon.Sql,InstallSqlState.Sql and InstallSqlTemplate.Sql.The code web.config file is:-
  1. <sessionState mode="SQLServer" cookieless="true" regenerateExpiredSessionId="true" allowCustomSqlDatabase="true" stateConnectionString="Data Source=DESKTOP-NT0TKOE\SQLEXPRESS;Initial Catalog=aspnetdb;Persistent Security=true;Integrated Security=SSPI;" timeout="60">  
  2. </sessionState>  
I have also set a field of bit data type which is set to zero by default such that when a user logges in it is updated to one and there concurrent login is denied due to the following code in Login.aspx.cs file:-
  1. TestEntities dbo = new TestEntities();  
  2. var flag = dbo.Logins.Where(u => u.Username == this.cntLogin.UserName && u.Password == this.cntLogin.Password).FirstOrDefault();  
  3. if (flag != null)  
  4. {  
  5. var temp = dbo.Tokens.Where(u => u.Username == this.cntLogin.UserName && u.Tokenvalue == false).FirstOrDefault();  
  6. if (temp != null)  
  7. {  
  8. HttpContext.Current.Session["Username"] = this.cntLogin.UserName;  
  9. HttpContext.Current.Session["Password"] = this.cntLogin.Password;  
  10. FormsAuthentication.RedirectFromLoginPage(this.cntLogin.UserName, this.cntLogin.RememberMeSet);  
  11. }  
  12. }  
  13. else  
  14. {  
  15. this.cntLogin.FailureText = "Username and/or password is incorrect."; where cntLogin is the Id of asp:Login control.  
  16. }  
Can you help me out??

Answers (4)