I have implemented the multiple device logout functionality. if someone changes the password from one device than if the same user is logged in other device than he should be automatically gets log out by clicking on any other page.
Below is the approach I have implemented it. Step 1: I have created the below static class to achieve it.
enter code herepublic static class SessionStateStoreProvider
{
public static List<SessionStateList> sessionStateProviderList;
static SessionStateStoreProvider()
{
sessionStateProviderList = new List<SessionStateList>();
}
}
public class SessionStateList
{
public string UserName { get; set; }
public string GUID { get; set; }
}
Step 2: On each successfull login i am storing it in SessionStateStoreProvider class.
Guid sessionIdentifier = Guid.NewGuid();
SessionStateList objSessionState = new SessionStateList();
objSessionState.GUID = Convert.ToString(sessionIdentifier);
objSessionState.UserName = userName;
SessionStateStoreProvider.sessionStateProviderList.Add(objSessionState);
Session["SessionIdentifier"] = Convert.ToString(sessionIdentifier);
Step 3 : On successfully changing the password i am removing all the Guid's associated to the user.
SessionStateStoreProvider.sessionStateProviderList.RemoveAll(x => x.UserName == userName);
Step 4 : I have created one Http Module which checks that the user request is valid or not.
string sessionIdentifier = Convert.ToString(HttpContext.Current.Session["SessionIdentifier"]);
string userName = HttpContext.Current.User.Identity.Name;
List<SessionStateList> stateList = SessionStateStoreProvider.sessionStateProviderList;
bool isValidSession = false; isValidSession = stateList.Any(cus => cus.GUID == sessionIdentifier);
if (isValidSession == false)
{
FederatedAuthentication.SessionAuthenticationModule.SignOut();
}
This works fine in my testing Add to dictionary as there is only one server handling all the request. But it is not working in my production environment as there are 3 different WFE's handling the web request. From my log I found that each login request is being handled by different WFE's. I was logged in with 4 different browser at the same time but SessionStateProviderList logged only one active session.
int numberOfActiveSession = 0;
numberOfActiveSession = Convert.ToInt32(SessionStateStoreProvider.sessionStateProviderList.FindAll(x => x.UserName == userName).Count);
How do i handle this scenario ? Which approach should I follow ? Does caching is the solution to this problem ? Any help would be highly appreciated.