Beispiel #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
            }
        }