// API interaction /// <summary> /// Logs into the specified MediaWiki API endpoint. /// </summary> /// <param name="wiki">The <see cref="Wiki"/> to contact.</param> /// <param name="userName">The user name to use for the login attempt.</param> /// <param name="password">The password to use for the login attempt.</param> /// <param name="onSuccess">A function that will be called when the login attempt is successful.</param> /// <param name="onError">A function that will be called when the login attempt fails. /// The function is passed the error message string as its only parameter.</param> public static void LogIn(Wiki wiki, string userName, string password, MorebitsDotNetLoginSuccess onSuccess, MorebitsDotNetError onError) { StringDictionary query = new StringDictionary { { "action", "login" }, { "lgname", userName }, { "lgpassword", password }, }; PostApi(wiki, query, delegate(XmlDocument doc) { XmlNode login = doc.SelectSingleNode("/api/login"); if (login.Attributes["result"].Value != "NeedToken") { onError(Localization.GetString("MorebitsDotNet_LoginFailure", login.Attributes["result"].Value)); return; } StringDictionary loginQuery = new StringDictionary { { "action", "login" }, { "lgname", userName }, { "lgpassword", password }, { "lgtoken", login.Attributes["token"].Value } }; PostApi(wiki, loginQuery, delegate(XmlDocument innerDoc) { XmlNode innerLogin = innerDoc.SelectSingleNode("/api/login"); LoginSessions[wiki].UserName = innerLogin.Attributes["lgusername"].Value; LoginSessions[wiki].UserID = innerLogin.Attributes["lguserid"].Value; LoginSessions[wiki].LoggedIn = true; onSuccess(); }, onError, true, WebRequestMethods.Http.Post); }, onError); }