/// <summary>
        /// Makes sure the Authentication Headers and CSRF Tokens are set.
        /// </summary>
        /// <returns>True if Authentication is successful, otherwise false.</returns>
        public static async Task <bool> EnsureAuthenticationAsync(DeviceInfo targetDevice)
        {
            targetDevice.Authorization["Authorization"] = Rest.GetBasicAuthentication(targetDevice.User, targetDevice.Password);

            bool success;

            if (!targetDevice.Authorization.ContainsKey("cookie"))
            {
                var response = await DevicePortalAuthorizationAsync(targetDevice);

                success = response.Successful;

                if (success)
                {
                    // If null, authentication succeeded but we had no cookie token in the response.
                    // This usually means Unity has a cached token, so it can be ignored.
                    if (response.ResponseBody != null)
                    {
                        targetDevice.CsrfToken = response.ResponseBody;

                        // Strip the beginning of the cookie header
                        targetDevice.CsrfToken = targetDevice.CsrfToken.Replace("CSRF-Token=", string.Empty);
                    }
                }
                else
                {
                    Debug.LogError($"Authentication failed! {response.ResponseBody}");
                }

                if (!string.IsNullOrEmpty(targetDevice.CsrfToken))
                {
                    if (!targetDevice.Authorization.ContainsKey("cookie"))
                    {
                        targetDevice.Authorization.Add("cookie", targetDevice.CsrfToken);
                    }
                    else
                    {
                        targetDevice.Authorization["cookie"] = targetDevice.CsrfToken;
                    }

                    if (targetDevice.Authorization.ContainsKey("x-csrf-token"))
                    {
                        targetDevice.Authorization["x-csrf-token"] = targetDevice.CsrfToken;
                    }
                    else
                    {
                        targetDevice.Authorization.Add("x-csrf-token", targetDevice.CsrfToken);
                    }
                }
            }
            else
            {
                success = true;
            }

            return(success);
        }
コード例 #2
0
        /// <summary>
        /// Makes sure the Authentication Headers and CSRF Tokens are set.
        /// </summary>
        /// <param name="targetDevice"></param>
        /// <returns>True if Authentication is successful, otherwise false.</returns>
        public static async Task <bool> EnsureAuthenticationAsync(DeviceInfo targetDevice)
        {
            string auth = Rest.GetBasicAuthentication(targetDevice.User, targetDevice.Password);

            if (targetDevice.Authorization.ContainsKey("Authorization"))
            {
                targetDevice.Authorization["Authorization"] = auth;
            }
            else
            {
                targetDevice.Authorization.Add("Authorization", auth);
            }

            bool success;

            if (!targetDevice.Authorization.ContainsKey("cookie"))
            {
                var response = await DevicePortalAuthorizationAsync(targetDevice);

                success = response.Successful;

                if (success)
                {
                    targetDevice.CsrfToken = response.ResponseBody;

                    // Strip the beginning of the cookie header
                    targetDevice.CsrfToken = targetDevice.CsrfToken.Replace("CSRF-Token=", string.Empty);
                }

                if (!string.IsNullOrEmpty(targetDevice.CsrfToken))
                {
                    if (!targetDevice.Authorization.ContainsKey("cookie"))
                    {
                        targetDevice.Authorization.Add("cookie", targetDevice.CsrfToken);
                    }
                    else
                    {
                        targetDevice.Authorization["cookie"] = targetDevice.CsrfToken;
                    }

                    if (targetDevice.Authorization.ContainsKey("x-csrf-token"))
                    {
                        targetDevice.Authorization["x-csrf-token"] = targetDevice.CsrfToken;
                    }
                    else
                    {
                        targetDevice.Authorization.Add("x-csrf-token", targetDevice.CsrfToken);
                    }
                }
            }
            else
            {
                success = true;
            }

            return(success);
        }