Пример #1
0
        /// <summary>
        /// Create authentication ticket and save into cookie
        /// </summary>
        /// <param name="userName">UserName of authenticated user</param>
        /// <param name="token">Role of authenticated user</param>
        public static void SignIn(string userName, string token)
        {
            int sessionTimeOut = 60;

            //Created authentication ticket
            var authTicket = new FormsAuthenticationTicket(userName, true, sessionTimeOut);

            //Encrypt authentication ticket
            string cookieContents = FormsAuthentication.Encrypt(authTicket);

            //Create cookie for authentication ticket
            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieContents)
            {
                Path = FormsAuthentication.FormsCookiePath
            };

            //Save authentication ticket into cookie
            if (System.Web.HttpContext.Current != null)
            {
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
                var tokenKey = ApplicationSettings.Token;
                Cookies.AddCookies(tokenKey, token);
            }
        }
Пример #2
0
        protected override Cookies GetCookies(ISpider spider)
        {
            if (string.IsNullOrEmpty(User) || string.IsNullOrEmpty(Password) || UserSelector == null || PasswordSelector == null)
            {
                throw new SpiderException("Arguments of WebDriverCookieInjector are incorrect.");
            }
            var cookies = new Dictionary <string, string>();

            var webDriver = CreateWebDriver();

            try
            {
                webDriver.Navigate().GoToUrl(Url);
                Thread.Sleep(10000);

                BeforeInputInfo(webDriver);

                if (UserSelector != null)
                {
                    var user = FindElement(webDriver, UserSelector);
                    user.Clear();
                    user.SendKeys(User);
                    Thread.Sleep(1500);
                }

                if (PasswordSelector != null)
                {
                    var pass = FindElement(webDriver, PasswordSelector);
                    pass.Clear();
                    pass.SendKeys(Password);
                    Thread.Sleep(1500);
                }

                var submit = FindElement(webDriver, SubmitSelector);
                submit.Click();
                Thread.Sleep(10000);

                AfterLoginComplete(webDriver);

                var cookieList = webDriver.Manage().Cookies.AllCookies.ToList();
                if (cookieList.Count > 0)
                {
                    foreach (var cookieItem in cookieList)
                    {
                        cookies.Add(cookieItem.Name, cookieItem.Value);
                    }
                }

                webDriver.Dispose();
            }
            catch (Exception e)
            {
                Logger.AllLog(spider.Identity, "Get cookie failed.", NLog.LogLevel.Error, e);
                webDriver.Dispose();
                return(null);
            }

            var result = new Cookies();

            result.AddCookies(cookies, new Uri(Url).Host);
            return(result);
        }