How to develop a single-point logon System for asp.net and asp.net
Single Sign-On System (SSO. The following is a simple implementation that I spent a few hours writing. I would like to share with you the Implementation ideas.
Background: A project uses ASP. NET MemberShip as its MemberShip system and needs to log on to multiple systems at the same time. Project developers cannot perform single-point logon based on MemberShip.
Therefore, this single sign-on system must be seamlessly integrated into any system in these systems as a server.
Next let's take a look at the specific implementation steps, do not have patience to read the friends can directly go to github for reference :( https://github.com/newmin/cms/blob/master/extend/Ops.Cms.Extend/SSO ):
I. Main Methods for defining SSO:
1. The client must include: logon, logout, and session retrieval.
1 using System; 2 using System. web; 3 using Ops. cms. extend. SSO; 4 5 namespace Ops. cms. extend. SSO. client 6 {7 public interface ISessionClient 8 {9 // <summary> 10 // process SSO request 11 /// </summary> 12 /// <param name =" context "> </param> 13 void HandleSsoRequest (HttpContext context ); 14 15 /// <summary> 16 // get session 17 /// </summary> 18 /// <param name = "sessionKey"> </param> 19/ // <returns> </returns> 20 SessionResult GetSession (String sessionKey ); 21 22 /// <summary> 23 // obtain the session Key24 /// </summary> 25 /// <returns> </returns> 26 String GetSessionKey (); 27 28 /// <summary> 29 // synchronous login 30 /// </summary> 31 /// <param name = "user"> </param> 32/ // <param name = "pwd"> </param> 33 // <returns> </returns> 34 SsoResult Login (String user, string pwd ); 35 36 /// <summary> 37 // log out 38 /// </summary> 39 /// <param name = "sessionKey"> </param> 40/ // <returns> </returns> 41 SsoResult Logout (String sessionKey ); 42} 43}
2. The server needs to process client requests, including session management and client registration.
Using System; using System. collections. generic; using System. text; using System. web; using Ops. framework. extensions; namespace Ops. cms. extend. SSO. server {public class SessionServer {// <summary> // register the client // </summary> /// <param name = "url"> </param> public void RegisterClient (String url) /// <summary> /// session management /// </summary> public SessionManager /// <summary> /// session service response /// </summary> public String Process (HttpContext context) /// <summary> /// log on /// </summary> /// <param name = "usr"> </param> /// <param name =" pwd "> </param> /// <returns> </returns> private SsoResult Login (string usr, string pwd) /// <summary> /// log out /// </summary> /// <param name = "sessionKey"> </param> /// <returns> </returns> private SsoResult LoginOut (string sessionKey )}}
2.1 Client Registration
Register the client. When you return the logon/logout result, all clients are requested for synchronization.
2.2 session management
The relationship between members and session keys needs to be stored. In this implementation, I used the KV database and LevelDb to support it.
ISessionSet is defined at the same time, which means that sessions can be stored in any way, as long as the ISessionSet interface is implemented
ISessionSet. cs
Namespace Ops. cms. extend. SSO {// <summary> /// session data set /// </summary> public interface ISessionSet {/// <summary> /// update session information /// </summary> /// <param name = "key"> </param> /// <param name = "value"> </param> /// <returns> returns the previous session information </returns> string Put (string key, string value ); /// <summary> /// Delete session information /// </summary> /// <param name = "key"> </param> void Delete (string key); /// <summary> /// obtain session information /// </summary> /// <param name = "key"> </param> string Get (string key) ;}}
3. Verified Member
Because the member does not exist in this SSO, the delegate is defined. When initializing the server, you can pass in the delegate.
Namespace Ops. cms. extend. SSO {// <summary> /// login processing /// </summary> /// <param name = "usr"> </param> // <param name = "pwd"> </param> // <returns> returns the personId </returns> public delegate int SSOLoginHandler (string usr, string pwd );}
The int value returned by the delegate is the member number. Therefore, we need to define a delegate to obtain member information.
Namespace Ops. cms. extend. SSO {/// <summary> /// obtain and handle the event // </summary> /// <param name = "personId"> </param> /// <returns> </returns> public delegate Person PersonFetchHandler (int personId );}
4. Start the server
So far, we have completed the basic implementation.
PS: I haven't posted a blog for a long time. It's so hard. Directly attach the DEMO:
Https://github.com/newmin/cms/blob/master/extend/Ops.Cms.Extend/SSO/Demo/server.ashx.cs