Login result returned by Authenticator.Login method, can be used to determine if login was successful and display the error message if it was not.
Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <c>LoginEventArgs</c> class.
 /// </summary>
 /// <param name="result"></param>
 internal LoginEventArgs(LoginResult result)
 {
     Result = result;
 }
        /// <summary>
        /// Sends a login request to the minecraft authentication server
        /// and returns the response from it.
        /// </summary>
        /// <param name="username">Minecraft Username</param>
        /// <param name="password">Minecraft Password</param>
        /// <returns><see cref="LoginResult" /> struct containing details about the result.</returns>
        public LoginResult Login(string username, string password)
        {
            string data = string.Format(AuthData, username, password, _version);

            // Create HTTPS POST request
            _log.Debug("Creating HTTPS request...");
            var request = (HttpWebRequest) WebRequest.Create(AuthAddress);
            request.Method = "POST";
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version11;

            // Turn string data into byte array
            byte[] postData = Encoding.ASCII.GetBytes(data);

            // Specify type
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postData.Length;

            // Get stream
            Stream stream;
            try
            {
                _log.Debug("Getting request stream...");
                stream = request.GetRequestStream();
            }
            catch(WebException ex)
            {
                _log.Warn("Failed to contact " + AuthAddress + ". " + ex.GetType() + ": " + ex.Message);
                var result = new LoginResult(false, "Failed to contact " + AuthAddress + ". "
                                                    + ex.GetType() + " thrown with message: " + ex.Message);
                LoginEvent(new LoginEventArgs(result));
                return result;
            }

            // Send request
            _log.Info("Sending HTTPS POST request to " + AuthAddress);
            stream.Write(postData, 0, postData.Length);
            stream.Close();

            // Get response
            HttpWebResponse response;
            try
            {
                _log.Debug("Retrieving response from request...");
                response = (HttpWebResponse) request.GetResponse();
            }
            catch (WebException ex)
            {
                _log.Warn("Failed to retrieve response from " + AuthAddress + ". " + ex.GetType() + ": " + ex.Message);
                var result = new LoginResult(false, "Failed to retrieve response from " + AuthAddress + ". "
                                            + ex.GetType() + " thrown with message: " + ex.Message);
                LoginEvent(new LoginEventArgs(result));
                return result;
            }

            _log.Debug("Creating response stream...");
            Stream responseStream = response.GetResponseStream();
            if (responseStream == null)
            {
                _log.Warn("Response stream was NULL. Login failed.");
                var result = new LoginResult(false, "Response stream was NULL. Login failed.");
                LoginEvent(new LoginEventArgs(result));
                return result;
            }

            _log.Debug("Creating response reader...");
            var reader = new StreamReader(responseStream);
            string responseString = reader.ReadToEnd();

            _log.Info("Got response: " + responseString);

            string[] responseArray = responseString.Split(':');

            if (responseArray.Length < 4)
            {
                _log.Warn("Incorrect response format, expected 4 fields of data, got " + responseArray.Length);
                _log.Warn("Login failed.");
                var result = new LoginResult(false, "Invalid response data size, expected 4, got " + responseArray.Length +
                                            ".\nResponse was:\n" + responseString);
                LoginEvent(new LoginEventArgs(result));
                return result;
            }

            LoginEvent(new LoginEventArgs(new LoginResult(true, responseString, responseArray[2], responseArray[3])));
            return new LoginResult(true, responseString, responseArray[2], responseArray[3]);
        }
Exemple #3
0
 /// <summary>
 /// Initializes a new instance of the <c>LoginEventArgs</c> class.
 /// </summary>
 /// <param name="result"></param>
 internal LoginEventArgs(LoginResult result)
 {
     Result = result;
 }