ASP.Net MVC5 SSO单点登录

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




实现效果:

甲A账号在Google登录,乙A账号在FireFox登录,甲会被强制下线(同一账号不能同时在线)

SSOHelper帮助类:

public class SSOHelper
    {
        /// <summary>
        /// 是否已经退出
        /// </summary>
        public bool isLogout { get; set; }

        /// <summary>
        /// 确认userId的唯一性
        /// </summary>
        /// <param name="userId"></param>
        public static void isLogined(string userId)
        {
            HttpContext httpContext = System.Web.HttpContext.Current;
            Hashtable hOnline = (Hashtable)httpContext.Application["Online"];
            if (hOnline != null)
            {
                int i = 0;
                while (i < hOnline.Count)
                {
                    IDictionaryEnumerator idE = hOnline.GetEnumerator();
                    string strKey = "";
                    while (idE.MoveNext())
                    {
                        if (idE.Value != null && idE.Value.ToString().Equals(userId))
                        {
                            strKey = idE.Key.ToString();
                            hOnline[strKey] = "Offline";
                            break;
                        }
                    }
                    i = i + 1;
                }
            }
            else
            {
                hOnline = new Hashtable();
            }
            hOnline[httpContext.Session.SessionID] = userId;
            httpContext.Application.Lock();
            httpContext.Application["Online"] = hOnline;
            httpContext.Application.UnLock();
        }
    }

登录传入唯一Id值:

        public ActionResult Login(string op,string username, string pwd,string token)
        {
          SSOHelper sso = new SSOHelper();
            if (op=="login")
            {
                //假设用户名为"admin",密码为"123456"  
                if (username == "admin" && pwd == "123456")
                {
                    SSOHelper.isLogined(username);
                }
            }
            else
            {
                return View();
            }
            return Json(sso);
            //get请求需要修改成这样
            //return Json(result,JsonRequestBehavior.AllowGet);
        }

前台页面调用:

public ActionResult CheckIsForcedLogout()
        {
            try
            {
                HttpContext httpContext = System.Web.HttpContext.Current;
                Hashtable hOnline = (Hashtable)httpContext.Application["Online"];
                if (hOnline != null)
                {
                    IDictionaryEnumerator idE = hOnline.GetEnumerator();
                    while (idE.MoveNext())
                    {
                        if (idE.Key != null && idE.Key.ToString().Equals(httpContext.Session.SessionID))
                        {
                            if (idE.Value != null && "Offline".Equals(idE.Value.ToString()))
                            {
                                hOnline.Remove(Session.SessionID);
                                httpContext.Application.Lock();
                                httpContext.Application["Online"] = hOnline;
                                httpContext.Application.UnLock();
                                sso.isLogout = true;
                                return Json(sso);
                            }
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            sso.isLogout = false ;
            return Json(sso);
        }

Global.asax

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
        protected void Session_Start(object sender, EventArgs e)
        {

        }
        protected void Session_End(object sender, EventArgs e)
        {
            Hashtable hOnline = (Hashtable)Application["Online"];
            if (hOnline[Session.SessionID] != null)
            {
                hOnline.Remove(Session.SessionID);
                Application.Lock();
                Application["Online"] = hOnline;
                Application.UnLock();
            }
        }
        protected void Application_End(object sender, EventArgs e)
        {

        }
    }

前端

    $(document).ready(function () {
        $.ajax({
            type: 'Post',
            url: '/CheckIsForcedLogout',
            dataType: 'json', //返回类型
            success: function (data) { //请求成功
                if (data["isLogout"]) {
                    alert("您被迫下线");
                    window.location.href = "/Login";
                }
            },
            error: function (XMLHttpRequest, textStatus) { //请求失败
                window.location.href = "/Login";
            }
        });
    });

原文链接:https://www.idaobin.com/archives/2013.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

70 − 69 =