public static void AuthenticateRequestDecryptCustomFormsAuthenticationTicket(HttpContext httpContext)
        {
            UserData userData;

            string formsCookieName = FormsAuthentication.FormsCookieName;
            HttpCookie httpCookie =
                httpContext.Request.Cookies[
                    string.IsNullOrWhiteSpace(formsCookieName) ? Guid.NewGuid().ToString() : formsCookieName];
            if (httpCookie == null)
            {
                userData = new UserData();
            }
            else
            {
                FormsAuthenticationTicket authenticationTicket = FormsAuthentication.Decrypt(httpCookie.Value);

                if (!UserData.TryParse(authenticationTicket.UserData, out userData))
                {
                    // No name will mean the Idenity has no name .. which means the user is NOT authenticated. Nice.
                    userData = new UserData();
                }
            }

            var principal = new CustomPrincipal(new CustomIdentity(userData.Id, userData.DisplayName), null);
            httpContext.User = principal;
            Thread.CurrentPrincipal = principal;
        }
예제 #2
0
        public static bool TryParse(string data, out UserData userData)
        {
            if (string.IsNullOrWhiteSpace("data"))
            {
                throw new ArgumentNullException("data");
            }

            userData = null;

            // Split the text into segments.
            string[] segments = data.Split(new[] { Delimeter }, StringSplitOptions.RemoveEmptyEntries);
            if (segments.Length != 2)
            {
                return false;
            }

            int id;
            int.TryParse(segments[0], out id);

            userData = new UserData
            {
                Id = id,
                DisplayName = segments[1]
            };

            return true;
        }
        public void SignIn(string id, string displayName, HttpResponseBase httpResponseBase)
        {
            var userData = new UserData
            {
                Id = id,
                DisplayName = displayName
            };

            string encodedTicket =
                FormsAuthentication.Encrypt(new FormsAuthenticationTicket(1, displayName, DateTime.UtcNow,
                                                                          DateTime.UtcNow.Add(
                                                                              FormsAuthentication.Timeout),
                                                                          true,
                                                                          userData.ToString()));
            var httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encodedTicket);
            httpResponseBase.Cookies.Add(httpCookie);
        }