/// <summary> /// Saves the current sign-in information (username+token) as a browser cookie /// and to local storage so automatic sign-in can occur with the next session. If the user is not /// signed in, removes the token from local storage - keeps the username to /// seed the sign-in dialog. /// </summary> void SaveTokenToStorage() { if (Current != null) // success { // save the cookie // CachedSignIn signIn = new CachedSignIn() { Email = Current.Username, FullName = Current.FullName, Token = this.Token }; string json = WebUtil.SaveObject <CachedSignIn>(signIn); WebUtil.SetCookie("esri_auth", HttpUtility.UrlEncode(json), ArcGISOnlineEnvironment.HostDomain, "/", 14); } else { WebUtil.DeleteCookie("esri_auth", ArcGISOnlineEnvironment.HostDomain); } // save to username to isolated storage to seed the sign-in dialog later // if (!IsolatedStorageFile.IsEnabled || Current == null) { return; } IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; if (settings.Contains("username")) { settings["username"] = Current.Username; } else { settings.Add("username", Current.Username); } settings.Save(); }
/// <summary> /// Saves the current sign-in information (username+token) as a browser cookie /// and to local storage so automatic sign-in can occur with the next session. If the user is not /// signed in, removes the token from local storage - keeps the username to /// seed the sign-in dialog. /// </summary> void SaveTokenToStorage() { if (Current != null) // success { // save the cookie // CachedSignIn signIn = new CachedSignIn() { Email = Current.Username, FullName = Current.FullName, Token = this.Token }; string json = WebUtil.SaveObject<CachedSignIn>(signIn); WebUtil.SetCookie("esri_auth", HttpUtility.UrlEncode(json), ArcGISOnlineEnvironment.HostDomain, "/", 14); } else WebUtil.DeleteCookie("esri_auth", ArcGISOnlineEnvironment.HostDomain); // save to username to isolated storage to seed the sign-in dialog later // if (!IsolatedStorageFile.IsEnabled || Current == null) return; IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; if (settings.Contains("username")) settings["username"] = Current.Username; else settings.Add("username", Current.Username); settings.Save(); }
/// <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 } }