Esempio n. 1
0
        /// <summary>
        /// Initiates sign-in based on the username and token stored in user settings.
        /// </summary>
        public void SignInFromLocalStorage(EventHandler <RequestEventArgs> callback)
        {
            string username = null;
            string token    = null;

            // see if the browser cookie is present
            //
            string cookie = WebUtil.GetCookie("esri_auth");

            if (cookie != null)
            {
                cookie = HttpUtility.UrlDecode(cookie);
                CachedSignIn signIn = WebUtil.ReadObject <CachedSignIn>(new MemoryStream(Encoding.UTF8.GetBytes(cookie)));
                username = signIn.Email;
                token    = signIn.Token;
            }

            if (username != null && token != null)
            {
                string url = _agol.Url + "community/users/" + username + "?token=" + token + "&f=json";
                WebUtil.OpenReadAsync(url, null, (sender, e) =>
                {
                    if (e.Error != null) // bail on error
                    {
                        if (callback != null)
                        {
                            callback(null, new RequestEventArgs()
                            {
                                Error = e.Error
                            });
                        }
                        return;
                    }

                    User cachedUser = WebUtil.ReadObject <User>(e.Result);

                    if (cachedUser != null && !string.IsNullOrEmpty(cachedUser.Username))
                    {
                        // Add credential to IdentityManager
                        if (!string.IsNullOrEmpty(token) && !string.IsNullOrEmpty(username) &&
                            IdentityManager.Current != null &&
                            IdentityManager.Current.FindCredential(_agol.Url, username) == null)
                        {
                            IdentityManager.Credential cred = new IdentityManager.Credential()
                            {
                                UserName = username,
                                Url      = _agol.Url,
                                Token    = token
                            };
                            IdentityManager.Current.AddCredential(cred);
                        }

                        Current = cachedUser;
                        Token   = token;
                    }
                    else
                    {
                        Current = null;
                        Token   = null;
                    }

                    if (callback != null) // notify caller of success
                    {
                        callback(null, new RequestEventArgs());
                    }

                    if (SignedInOut != null)
                    {
                        SignedInOut(null, EventArgs.Empty);
                    }
                });
            }
            else if (callback != null)
            {
                callback(null, new RequestEventArgs()); // call the client back in any case
            }
        }
Esempio n. 2
0
        private void getGroups(EventHandler <GroupsEventArgs> callback, bool publicOnly)
        {
            if (Current == null)
            {
                if (callback != null)
                {
                    callback(null, new GroupsEventArgs()
                    {
                        Error = new Exception(ESRI.ArcGIS.Mapping.Controls.ArcGISOnline.Resources.Strings.ExceptionNoUserSignedIn)
                    });
                }
                return;
            }

            string accountId = Current.AccountId;

            string url = null;

            if (publicOnly)
            {
                url = _agol.Url + "community/groups?q=(accountid:" + accountId + "%20AND%20(access:public))&sortField=title&sortOrder=asc&start=1&num=100&f=json&token=" + Token;
            }
            else
            {
                url = _agol.Url + "community/groups?q=(accountid:" + accountId + "%20AND%20(access:account%20||%20access:public))&sortField=title&sortOrder=asc&start=1&num=100&f=json&token=" + Token;
            }

            // add a bogus parameter to avoid the caching that happens with the WebClient
            //
            url += "&tickCount=" + Environment.TickCount.ToString();

            WebUtil.OpenReadAsync(url, null, (sender, e) =>
            {
                if (e.Error != null)
                {
                    if (callback != null)
                    {
                        callback(this, new GroupsEventArgs()
                        {
                            Error = e.Error
                        });
                    }
                    return;
                }

                GroupResults result = WebUtil.ReadObject <GroupResults>(e.Result);

                if (callback != null && result != null && result.Groups != null)
                {
                    callback(null, new GroupsEventArgs()
                    {
                        Groups = result.Groups
                    });
                }
                else if (callback != null)
                {
                    callback(null, new GroupsEventArgs()
                    {
                        Error = new Exception(ESRI.ArcGIS.Mapping.Controls.ArcGISOnline.Resources.Strings.ExceptionFailedToRetrieveOrgGroups)
                    });
                }
            });
        }
Esempio n. 3
0
 internal void DeleteTokenFromStorage()
 {
     WebUtil.DeleteCookie("esri_auth", ArcGISOnlineEnvironment.HostDomain);
 }
        /// <summary>
        /// Gets the service information for a MapService asynchronously.
        /// </summary>
        public static void GetServiceInfoAsync(string url, object userState, EventHandler <ServiceEventArgs> callback)
        {
            WebUtil.OpenReadAsync(new Uri(url + "?f=json"), null, (sender, e) =>
            {
                if (e.Error != null)
                {
                    callback(null, new ServiceEventArgs());
                    return;
                }

                MapService mapService = WebUtil.ReadObject <MapService>(e.Result);
                if (mapService != null && mapService.Name != null && mapService.Units != null)
                {
                    mapService.Url           = url;
                    mapService.RequiresProxy = e.UsedProxy;
                    mapService.InitTitle();
                    callback(null, new ServiceEventArgs()
                    {
                        Service = mapService, UserState = userState
                    });
                    return;
                }

                FeatureService featureService = WebUtil.ReadObject <FeatureService>(e.Result);
                if (featureService != null && featureService.Layers != null && featureService.Layers.Length > 0)
                {
                    featureService.Url           = url;
                    featureService.RequiresProxy = e.UsedProxy;
                    featureService.InitTitle();
                    callback(null, new ServiceEventArgs()
                    {
                        Service = featureService, UserState = userState
                    });
                    return;
                }

                ImageService imageService = WebUtil.ReadObject <ImageService>(e.Result);
                if (imageService != null && imageService.PixelType != null)
                {
                    imageService.Url           = url;
                    imageService.RequiresProxy = e.UsedProxy;
                    imageService.InitTitle();
                    callback(null, new ServiceEventArgs()
                    {
                        Service = imageService, UserState = userState
                    });
                    return;
                }

                FeatureLayerService featureLayerService = WebUtil.ReadObject <FeatureLayerService>(e.Result);
                if (featureLayerService != null && featureLayerService.Type == "Feature Layer")
                {
                    featureLayerService.Url           = url;
                    featureLayerService.RequiresProxy = e.UsedProxy;
                    featureLayerService.Title         = featureLayerService.Name;
                    callback(null, new ServiceEventArgs()
                    {
                        Service = featureLayerService, UserState = userState
                    });
                    return;
                }

                callback(null, new ServiceEventArgs());
            });
        }