public void AuthenticateRequestDecryptCustomFormsAuthenticationTicket(HttpContext httpContext)
        {
            AdminData adminData;

            string     formsCookieName = FormsAuthentication.FormsCookieName;
            HttpCookie httpCookie      = httpContext.Request.Cookies[(String.IsNullOrWhiteSpace(formsCookieName)) ? Guid.NewGuid().ToString() : formsCookieName];

            if (httpCookie == null)
            {
                adminData = new AdminData();
            }
            else
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(httpCookie.Value);

                if (!AdminData.TryParse(ticket.UserData, out adminData))
                {
                    adminData = new AdminData();
                }

                CustomPrincipal principal = new CustomPrincipal(new CustomIdentity(adminData.AdminId, adminData.UserName, adminData.FirstName, adminData.LastName, adminData.IsSystemUser), null);
                httpContext.User        = principal;
                Thread.CurrentPrincipal = principal;
            }
        }
        public void SignIn(int adminId, string userName, string firstName, string lastName, bool isSystemUser, System.Web.HttpResponseBase httpResponseBase)
        {
            AdminData adminData = new AdminData
            {
                AdminId      = adminId,
                UserName     = userName,
                LastName     = lastName,
                FirstName    = firstName,
                IsSystemUser = isSystemUser
            };

            string encodedTicket = FormsAuthentication.Encrypt(
                new FormsAuthenticationTicket(
                    version: 1,
                    name: userName,
                    issueDate: DateTime.UtcNow,
                    expiration: DateTime.UtcNow.Add(FormsAuthentication.Timeout),
                    isPersistent: true,
                    userData: adminData.ToString())
                );

            HttpCookie httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encodedTicket);

            httpResponseBase.Cookies.Add(httpCookie);
        }
        public void AuthenticateRequestDecryptCustomFormsAuthenticationTicket(HttpContext httpContext)
        {
            AdminData adminData;

            string formsCookieName = FormsAuthentication.FormsCookieName;
            HttpCookie httpCookie = httpContext.Request.Cookies[(String.IsNullOrWhiteSpace(formsCookieName)) ? Guid.NewGuid().ToString() : formsCookieName];

            if (httpCookie == null)
            {
                adminData = new AdminData();
            }
            else
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(httpCookie.Value);

                if (!AdminData.TryParse(ticket.UserData, out adminData))
                {
                    adminData = new AdminData();
                }

                CustomPrincipal principal = new CustomPrincipal(new CustomIdentity(adminData.AdminId, adminData.UserName, adminData.FirstName, adminData.LastName, adminData.IsSystemUser), null);
                httpContext.User = principal;
                Thread.CurrentPrincipal = principal;
            }
        }
예제 #4
0
        internal static bool TryParse(string data, out AdminData adminData)
        {
            if (string.IsNullOrWhiteSpace(data))
            {
                throw new ArgumentNullException("data");
            }

            adminData = null;

            string[] segments = data.Split(new string[] { Delimeter }, StringSplitOptions.RemoveEmptyEntries);
            if (segments.Length != 5)
            {
                return(false);
            }

            adminData = new AdminData()
            {
                AdminId      = int.Parse(segments[0]),
                UserName     = segments[1],
                FirstName    = segments[2],
                LastName     = segments[3],
                IsSystemUser = bool.Parse(segments[4])
            };

            return(true);
        }
        internal static bool TryParse(string data, out AdminData adminData)
        {
            if (string.IsNullOrWhiteSpace(data))
            {
                throw new ArgumentNullException("data");
            }

            adminData = null;

            string[] segments = data.Split(new string[] { Delimeter }, StringSplitOptions.RemoveEmptyEntries);
            if (segments.Length != 5)
            {
                return false;
            }

            adminData = new AdminData()
            {
                AdminId = int.Parse(segments[0]),
                UserName = segments[1],
                FirstName = segments[2],
                LastName = segments[3],
                IsSystemUser = bool.Parse(segments[4])
            };

            return true;
        }
        public void SignIn(int adminId, string userName, string firstName, string lastName, bool isSystemUser, System.Web.HttpResponseBase httpResponseBase)
        {
            AdminData adminData = new AdminData
            {
                AdminId = adminId,
                UserName = userName,
                LastName = lastName,
                FirstName = firstName,
                IsSystemUser = isSystemUser
            };

            string encodedTicket = FormsAuthentication.Encrypt(
                new FormsAuthenticationTicket(
                    version: 1,
                    name: userName,
                    issueDate: DateTime.UtcNow,
                    expiration: DateTime.UtcNow.Add(FormsAuthentication.Timeout),
                    isPersistent: true,
                    userData: adminData.ToString())
            );

            HttpCookie httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encodedTicket);
            httpResponseBase.Cookies.Add(httpCookie);
        }