コード例 #1
0
        /// <summary>
        /// Autehnticate given request
        /// </summary>
        public static bool CheckAuthentication(HttpRequest request)
        {
            bool result = false;

            // Get the ASP.NET authentication cookie
            var cookie = request.Cookies[FormsAuthentication.FormsCookieName];

            // Check wether the cookie was found
            if (cookie != null)
            {
                // decrypt the cookie
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                // if not expired
                if (!ticket.Expired)
                {
                    // if we got anything in the ticket
                    if (!string.IsNullOrEmpty(ticket.Name))
                    {
                        // if valid
                        string keyUser;
                        if (Identities.TryGetValue(ticket.Name, out keyUser))
                        {
                            // create a principal
                            var principal = new CustomPrincipal(keyUser, ticket.Name);

                            // set working principal into the working thread
                            Thread.CurrentPrincipal = principal;

                            // success
                            result = true;
                        }
                    }

                    // if authentication failed
                    if (!result)
                    {
                        // clear the ticket
                        FormsAuthentication.SignOut();
                    }
                }
            }
            return result;
        }
コード例 #2
0
        /// <summary>
        /// Authenticate given request
        /// </summary>
        public static bool Authenticate(string login)
        {
            bool result = false;

            // if we got anything in the ticket
            if (!string.IsNullOrEmpty(login))
            {
                // if valid
                string keyUser;
                if (Identities.TryGetValue(login, out keyUser))
                {
                    // create a principal
                    var principal = new CustomPrincipal(keyUser, login);

                    // set working principal into the working thread
                    Thread.CurrentPrincipal = principal;

                    // set the cookie
                    FormsAuthentication.SetAuthCookie(login, true);

                    // success
                    result = true;
                }
            }

            // if authentication failed
            if (!result)
            {
                // clear the ticket
                FormsAuthentication.SignOut();
            }
            return result;
        }