Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        protected void Application_AuthenticateRequest()
        {
            if (IgnoreAuthenticateRequest())
            {
                return;
            }

            try
            {
                WebUserPrincipal principal;
                if (User != null && User.Identity.IsAuthenticated && String.Compare(User.Identity.AuthenticationType, "Forms", false) == 0)
                {
                    WebUserData userData = WebUserData.FromCookieString(User.Identity.Name);
                    if (userData != null)
                    {
                        principal = new WebUserPrincipal(User.Identity, userData);
                    }
                    else
                    {
                        principal = WebUserPrincipal.Anonymous;
                    }
                    HttpContext.Current.User = Thread.CurrentPrincipal = principal;
                }
                else
                {
                    HttpContext.Current.User = Thread.CurrentPrincipal = WebUserPrincipal.Anonymous;
                }
            }
            catch
            {
                FormsAuthentication.SignOut();
                HttpContext.Current.User = Thread.CurrentPrincipal = WebUserPrincipal.Anonymous;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Lấy thông tin liên quan đến phiên đăng nhập của tài khoản
        /// </summary>
        /// <param name="userPrincipal"></param>
        /// <returns></returns>
        public static WebUserData GetUserData(this IPrincipal userPrincipal)
        {
            try
            {
                if (!userPrincipal.Identity.IsAuthenticated)
                {
                    return(null);
                }

                WebUserPrincipal principal = userPrincipal as WebUserPrincipal;
                if (principal == null)
                {
                    return(null);
                }
                else
                {
                    return(principal.UserData);
                }
            }
            catch
            {
                return(null);
            }
        }