Example #1
0
        /// <summary>
        /// Performs the authentication request.
        /// </summary>
        /// <returns>Returns a <see cref="AuthenticationResponse"/> object which contains the context identifier for this session.</returns>
        public override async Task <AuthenticationResponse> PerformRequestAsync()
        {
            this.Response = await Requester.PostAsync(this);

            if (!this.Response.IsSuccess)
            {
                try
                {
                    AuthenticationResponseError error = new AuthenticationResponseError(JObject.Parse(this.Response.RawResponse));
                    return(new AuthenticationResponse(this.Response)
                    {
                        Error = error
                    });
                }
                catch (Exception)
                {
                    return(new AuthenticationResponse(Error.GetError(this.Response)));
                }
            }

            else
            {
                Settings.Context = (string)JObject.Parse(this.Response.RawResponse)["data"]["contextID"];
                return(new AuthenticationResponse(this.Response)
                {
                    ContextID = Settings.Context
                });
            }
        }
Example #2
0
        /// <summary>
        /// 执行认证.
        /// </summary>
        public async override Task <AuthenticateResponse> PerformRequestAsync()
        {
            try
            {
                this.PostContent = new JObject(
                    new JProperty("agent",
                                  new JObject(
                                      new JProperty("name", "Minecraft"),
                                      new JProperty("version", "1"))),
                    new JProperty("username", this.Arguments[0]),
                    new JProperty("password", this.Arguments[1]),
                    new JProperty("clientToken", Requester.ClientToken),
                    new JProperty("requestUser", true)).ToString();

                this.Response = await Requester.Post(this);

                if (this.Response.IsSuccess)
                {
                    JObject     user = JObject.Parse(this.Response.RawMessage);
                    List <Uuid> availableProfiles = new List <Uuid>();

                    foreach (JObject profile in user["availableProfiles"])
                    {
                        var playerName = profile["name"].ToObject <string>();
                        var value      = profile["id"].ToObject <string>();
                        var legacy     = (profile.ContainsKey("legacyProfile") ? profile["legacyProfile"].ToObject <bool>() : false);
                        availableProfiles.Add(new Uuid()
                        {
                            PlayerName = playerName,
                            Value      = value,
                            Legacy     = legacy,
                            Demo       = null
                        });
                    }


                    return(new AuthenticateResponse(this.Response)
                    {
                        AccessToken = user["accessToken"].ToObject <string>(),
                        ClientToken = user["clientToken"].ToObject <string>(),
                        AvailableProfiles = availableProfiles,
                        SelectedProfile = new Uuid()
                        {
                            PlayerName = user["selectedProfile"]["name"].ToObject <string>(),
                            Value = user["selectedProfile"]["id"].ToObject <string>(),
                            Legacy = (user["selectedProfile"].ToString().Contains("legacyProfile") ? user["selectedProfile"]["legacyProfile"].ToObject <bool>() : false),
                            Demo = null
                        },
                        User = user["user"].ToObject <UserData>()
                    });
                }
                else
                {
                    try
                    {
                        AuthenticationResponseError error = new AuthenticationResponseError(JObject.Parse(this.Response.RawMessage));
                        return(new AuthenticateResponse(this.Response)
                        {
                            Error = error
                        });
                    }
                    catch (Exception)
                    {
                        return(new AuthenticateResponse(Error.GetError(this.Response)));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }