C # A class that stores user information and supports single sign-on

Source: Internet
Author: User
Tags delete key static class time limit

See here a lot of ask how to achieve a single sign-on, I according to their own experience, to provide a single sign-on class. Support for web and Winfrom, test success.

Use a hash table as a queue to save logged-in users
private static Hashtable m_userlist;
Set the time limit for users to timeout online (I set the 30 minutes, can be modified according to their own needs)
private static TimeSpan m_tssub = new TimeSpan (0, 30, 0);

To set the hash table to initialize to a synchronous package (for thread safety)
M_userlist = hashtable.synchronized (New Hashtable ());

Several ways are set up

1, login information in the database after the successful validation call Addusertolist (string userName, long SessionID)

2. Call the method Checkuserlogin (string userName, long SessionID) before all other function calls except for login

In the first way, you need to process "if the user has logged in, only update the login serial number and operation time, otherwise join the queue"

One of the most important steps in this class is to check whether the users saved in the queue exceed the time limit, this method is for the thread to call to check whether the user is online, needs to save the temporary queue of the timeout online user to be cleaned, the thread performs a check cleanup every 1 minutes, and traverses the hash table that holds the online user. If there is a timeout for online users, the timeout user is placed in a temporary queue,
After the hash table is checked, the cleanup operation is done.
The Delete key value operation of the hash table cannot be performed during the inspection process, otherwise an exception will occur.

The special note is that if there is a timeout for the online user to clean up the operation

The specific code is as follows
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Collections;
Using System.Threading;

Namespace Dllservicelibrary
{
Class Hashelement
{
Long M_sessionid;
DateTime M_time;

Public long SessionID
{
get {return m_sessionid;}
set {M_sessionid = value;}
}

Public DateTime Time
{
get {return m_time;}
set {m_time = value;}
}

Public Hashelement ()
{
M_sessionid =-1;
M_time = DateTime.Now;
}
}

public static class Userlogin
{
Hash table, as a queue to save logged-in users
private static Hashtable m_userlist;
A thread that checks whether the user has timed out online
private static Thread M_threadqueue;
User Time out of line timeout (30 minutes)
private static TimeSpan m_tssub = new TimeSpan (0, 30, 0);


public static Userlogin ()
public static void Init ()
{
Hash table initialized to synchronous encapsulation (thread safe)
M_userlist = hashtable.synchronized (New Hashtable ());
Initializing threads
M_threadqueue = new Thread (Checklistto);
M_threadqueue.isbackground = true; Set as Background thread
M_threadqueue.start ();
}

public static void Empty ()
{
if (m_threadqueue.isalive)
//{
M_threadqueue.abort ();
//}
M_userlist.clear ();
}

<summary>
The logon information is invoked after the validation is successful in the database.
</summary>
<param name= "UserName" > Username </param>
<param name= "SessionID" > Login serial number </param>
<returns>
0: User and login serial number successfully saved in the queue
-1: Save failed
</returns>
public static int Addusertolist (string userName, long SessionID)
{
Writelog.writedata w = new Writelog.writedata ();
int ret = 0;
Try
{
Hashelement Hashelt = new Hashelement ();
Hashelt.sessionid = SessionID;
If the user has already logged in, only the login serial number and operation time are updated, otherwise the new join queue
Lock (M_userlist.syncroot)
{
if (M_userlist.containskey (userName))
{
M_userlist[username] = Hashelt;
}
Else
{
M_userlist.add (UserName, Hashelt);
}
}
}
catch (Exception exp)
{
System.Console.Out.WriteLine (exp. StackTrace);
System.Console.Out.WriteLine (exp. message);
W.writeline (exp. Message + exp. StackTrace, 01);
ret =-1;
}
return ret;
}

<summary>
Call this method first before all other feature calls outside of the login
</summary>
<param name= "UserName" > Username </param>
<param name= "SessionID" > Login serial number </param>
<returns>
0: Verify User logon success
-1: Verify User Login failed
-2: User not logged in
-3: User has logged in again or logged in elsewhere
</returns>
public static int Checkuserlogin (string userName, long SessionID)
{
Writelog.writedata w = new Writelog.writedata ();
int ret = 0;
Try
{
Lock (M_userlist.syncroot)
{
if (M_userlist.containskey (userName))
{
&n

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.