예제 #1
0
		private void UpdateFomrResponse(AuthenticationResponse response)
		{
			AccessToken = Guid.Parse(response.AccessToken);
			if (response.User != null)
			{
				AccountType = response.User.Legacy ? "Legacy" : "Mojang";
				Properties = response.User.Properties != null
					? response.User.Properties.ToJson()
					: "{}";
			}
			else
			{
				AccountType = "Mojang";
				Properties = "{}";
			}
			DisplayName = response.SelectedProfile.Name;
			UUID = Guid.Parse(response.SelectedProfile.Id);
		}
예제 #2
0
        public Exception AuthToken(String accessToken, Guid uuid, string displayName)
        {
            lock (_locker)
            {
                Clear();
                if (string.IsNullOrWhiteSpace(displayName))
                {
                    return(new Exception("displayName为空"));
                }
                try
                {
                    WebRequest Http = WebRequest.Create(Auth_Validate);
                    Http.Method      = "POST";
                    Http.ContentType = "application/json";
                    Http.Timeout     = 100000;
                    var requestBody = JsonMapper.ToJson(new ValidateRequest
                    {
                        AccessToken = accessToken,
                        ClientToken = ClientToken.ToString("N"),
                    });
                    byte[] postdata = Encoding.UTF8.GetBytes(requestBody);
                    Http.GetRequestStream().Write(postdata, 0, postdata.Length);

                    using (HttpWebResponse hwr = (HttpWebResponse)Http.GetResponse())
                    {
                        if (Convert.ToInt32(hwr.StatusCode) == 204)
                        {
                            var LoginInfo = new AuthenticationResponse()
                            {
                                AccessToken     = accessToken,
                                ClientToken     = ClientToken.ToString("N"),
                                SelectedProfile = new GameProfile()
                                {
                                    Id   = uuid.ToString("N"),
                                    Name = displayName
                                }
                            };
                            UpdateFomrResponse(LoginInfo);
                            return(null);
                        }
                        else
                        {
                            using (StreamReader sr = new StreamReader(hwr.GetResponseStream()))
                            {
                                var response = JsonMapper.ToObject <Error>(sr.ReadToEnd());
                                return(new Exception(response.ErrorMessage));
                            }
                        }
                    }
                }

                catch (WebException ex)
                {
                    try
                    {
                        using (StreamReader sr = new StreamReader(((HttpWebResponse)ex.Response).GetResponseStream(), true))
                        {
                            var ErrorJson = JsonMapper.ToObject <Error>(sr.ReadToEnd());
                            return(new Exception(ErrorJson.ErrorMessage));
                        }
                    }
                    catch
                    {
                        return(ex);
                    }
                }
            }
        }