Пример #1
0
        public void RefreshUserInfo()
        {
            Reset();
            User = new UserInfo();

            string result = HttpPost(new[,] { { "action", "query" } },
                         new[,] {
                            { "meta", "userinfo" },
                            { "uiprop", "blockinfo|hasmsg|groups|rights" }
                         });

            var xml = CheckForErrors(result, "userinfo");

            User = new UserInfo(xml);
        }
Пример #2
0
 /// <summary>
 /// 
 /// </summary>
 private ApiEdit()
 {
     Cookies = new CookieContainer();
     User = new UserInfo();
     NewMessageThrows = true;
 }
Пример #3
0
 public void Logout()
 {
     Reset();
     User = new UserInfo();
     string result = HttpGet(new[,] { { "action", "logout" } });
     CheckForErrors(result, "logout");
 }
Пример #4
0
        public void Login(string username, string password)
        {
            if (string.IsNullOrEmpty(username)) throw new ArgumentException("Username required", "username");
            //if (string.IsNullOrEmpty(password)) throw new ArgumentException("Password required", "password");

            Reset();
            User = new UserInfo(); // we don't know for sure what will be our status in case of exception

            string result = HttpPost(new[,] { { "action", "login" } },
                                     new[,] 
                                     { 
                                        { "lgname", username }, 
                                        { "lgpassword", password }
                                     }
                                );

            XmlReader xr = XmlReader.Create(new StringReader(result));
            xr.ReadToFollowing("login");

            if (xr.GetAttribute("result").Equals("NeedToken", StringComparison.InvariantCultureIgnoreCase))
            {
                string token = xr.GetAttribute("token");

                result = HttpPost(new[,] { { "action", "login" } },
                                  new[,]
                                      {
                                          {"lgname", username},
                                          {"lgpassword", password},
                                          {"lgtoken", token}
                                      }
                    );

                xr = XmlReader.Create(new StringReader(result));
                xr.ReadToFollowing("login");
            }

            string status = xr.GetAttribute("result");

            if (!status.Equals("Success", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new LoginException(this, status);
            }

            CheckForErrors(result, "login");
            AdjustCookies();

            RefreshUserInfo();
        }
Пример #5
0
 public void Logout()
 {
     Reset();
     m_UserInfo = new UserInfo();
     string result = HttpGet(new[,] { { "action", "logout" } }, false);
     CheckForError(result, "logout");
 }
Пример #6
0
 private ApiEdit()
 {
     Cookies = new CookieContainer();
     User = new UserInfo();
 }
Пример #7
0
 public void Logout()
 {
     Reset();
     User = new UserInfo();
     string result = HttpGet(new Dictionary<string, string> {{"action", "logout"}});
     CheckForErrors(result, "logout");
     Cookies = new CookieContainer();
 }
Пример #8
0
        public void Login(string username, string password, string domain)
        {
            if (string.IsNullOrEmpty(username)) throw new ArgumentException("Username required", "username");
            // if (string.IsNullOrEmpty(password)) throw new ArgumentException("Password required", "password");

            Reset();
            User = new UserInfo(); // we don't know for sure what will be our status in case of exception
            Cookies = new CookieContainer();

            // first see if we can get a login token via the new MediaWiki way using action=query&meta=tokens&type=login
            string result = HttpPost(
                new Dictionary<string, string>
                {
                    {"action", "query"},
                    {"meta", "tokens"},
                    {"type", "login"}
                },
                new Dictionary<string, string>());

            Tools.WriteDebug("API::Edit meta/tokens", result);

            /* Result format: <query><tokens logintoken="b0fc31b291ebf9999a8e9a4bfac8ef0456c44116+\"/></query> */
            XmlReader xr = CreateXmlReader(result);
            xr.ReadToFollowing("tokens");
            string token = xr.GetAttribute("logintoken");

            // first log in. If we got a logintoken then use it, this should be our only action=login in that case
            bool domainSet = !string.IsNullOrEmpty(domain);
            var post = new Dictionary<string, string>
            {
                {"lgname", username},
                {"lgpassword", password},
            };
            post.AddIfTrue(domainSet, "lgdomain", domain);
            post.AddIfTrue(!string.IsNullOrEmpty(token), "lgtoken", token);

            result = HttpPost(
                new Dictionary<string, string>
                {
                    {"action", "login"}
                },
                post
                );

            xr = CreateXmlReader(result);

            Tools.WriteDebug("API::Edit action/login", result);

            // if got token from new meta/tokens way, should now be logged in
            if(!string.IsNullOrEmpty(token))
            {
                xr.ReadToFollowing("login");
            }
            else // support the old way of first action=login to be told NeedToken and given token, then second action=login sending the token
            {
                // if we have login section in warnings don't want to look in there for the token
                if(result.Contains("<warnings>") && Regex.Matches(result, @"<login ").Count > 1)
                {
                    xr.ReadToFollowing("warnings");
                    xr.ReadToFollowing("login");
                }

                xr.ReadToFollowing("login");

                var attribute = xr.GetAttribute("result");

                if (attribute != null && attribute.Equals("NeedToken", StringComparison.InvariantCultureIgnoreCase))
                {
                    AdjustCookies();
                    token = xr.GetAttribute("token");

                    post.Add("lgtoken", token);
                    result = HttpPost(
                        new Dictionary<string, string> {{"action", "login"}},
                        post
                        );

                    Tools.WriteDebug("API::Edit action/login NeedToken", result);
                    xr = CreateXmlReader(result);
                    xr.ReadToFollowing("login");
                }
            }

            string status = xr.GetAttribute("result");
            if (status != null && !status.Equals("Success", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new LoginException(this, status);
            }

            CheckForErrors(result, "login");
            AdjustCookies();

            RefreshUserInfo();
        }