Asp. NET Single Sign-on (code)

Source: Internet
Author: User
Tags httpcontext

For some reason, in our app we encounter a situation where a user can log in only one place, which is what we usually call Single sign-on. Implementing single Sign-On in ASP is simple, and the main method and all the code are analyzed below. [/P] [P=25, NULL, left] implementation ideas [/p][p=25, NULL, left] using the function of the cache, we keep the user's login information in the cache and set the expiration time for the session to expire, so once the session expires, Our cache expires, and the cache is accessible to all users, so it is easier to save user information than a database. [/P]
[P=25, NULL, left]string SKey = username. Text.tostring (). Trim (); Get the value of the given key in the cache

String suser = Convert.ToString (Cache[skey]); Check for Presence

if (suser = = NULL | | suser = = String.Empty)

{

TimeSpan sesstimeout = new TimeSpan (0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);//Get Session Expiration Time

HttpContext.Current.Cache.Insert (SKey, SKey, NULL, DateTime.MaxValue, Sesstimeout, System.Web.Caching.CacheItemPriority.NotRemovable, NULL);//Place the value in the cache and click

Successful Login

}

else if (Cache[skey]. ToString () = = SKey)//If this account is already logged in

{

Clientscript.registerstartupscript (GetType (), "hint", "<script>alert (' Sorry, current user is logged in ');</script>");

Return

}

Else

{

Session.Abandon ();//This paragraph is mainly to avoid unnecessary errors that can not log in

}

Asp. NET Single Sign-on (code)

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.