当在另一个选项卡中粘贴相同的URL时,强制从asp.net应用程序注销

时间:2017-11-29 12:52:00

标签: asp.net c#-4.0

我是这个认证机制的新手并且还在学习。我想要实现的是当用户尝试使用应用程序的多个实例时,比如通过打开不同的选项卡,应用程序应该自动注销。 / p>

我的登录代码

public static bool SignIn(int officeId, string userName, string password, bool remember, System.Web.UI.Page page)
    {


        int userId = BusinessLayer.Office.Users.GetUserId(userName);

        bool active = BusinessLayer.Office.Users.IsUserActive(userId);

        if(!active)
        {
            return false;
        }



        if (page != null)
        {
            try
            {
                string remoteAddress = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                string remoteUser = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_USER"];
                DateTime? serverDate = BusinessLayer.Core.DateConversion.GetCurrentServerDate();
                string pass = BusinessLayer.Security.Password.GetHash(password, userName);

                long logOnId = Everest.Net.DatabaseLayer.Security.User.SignIn(officeId, userName,
                                                BusinessLayer.Security.Password.GetHash(password, userName),
                                                page.Request.UserAgent, remoteAddress, remoteUser);
              //  var isActive = BusinessLayer.Office.Users.GetUserId(userName);


                if (logOnId > 0)
                {
                    if (IsBoDStarted(Conversion.TryCastDate(serverDate)))
                    {
                        var isSessionSet = SetSession(page, userName);
                        if (isSessionSet)
                        {
                            var ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now,
                                DateTime.Now.AddHours(12), remember, String.Empty, FormsAuthentication.FormsCookiePath);
                            string encryptedCookie = FormsAuthentication.Encrypt(ticket);
                            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookie);
                            cookie.Expires = DateTime.Now.AddHours(12);
                            page.Response.Cookies.Add(cookie);

                            System.Web.Security.FormsAuthentication.RedirectFromLoginPage(userName, true, "Everest.Net");
                            return true;
                        }
                    }
                    else
                    {
                        var ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now,
                            DateTime.Now.AddHours(12), remember, String.Empty, FormsAuthentication.FormsCookiePath);
                        string encryptedCookie = FormsAuthentication.Encrypt(ticket);
                        var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookie);
                        cookie.Expires = DateTime.Now.AddHours(12);
                        page.Response.Cookies.Add(cookie);

                        page.Response.Redirect("~/Utilities/BodOperation.aspx");
                        return true;
                    }
                }
            }
            catch (DbException)
            {
                //Swallow the exception
                return false;
            }
        }

        if (page != null)
        {
            page.Session.Clear();
        }
        return false;
    }

你这里的专家提出的任何想法都会有所帮助。我在SO和这里搜索了这个。很难掌握它。很高兴能帮助你。

我的webconfig

<authentication mode="Forms">
  <forms loginUrl="~/SignIn.aspx" timeout="6" slidingExpiration="true" defaultUrl="~/Index.aspx" />
</authentication>

在我上面的webconfig中,因为超时设置为6分钟,所以这也不起作用。

请帮助。

0 个答案:

没有答案