// Updates the logged-on user's data. public void UpdateLoggedOnUserData(UserData user) { if (user == null) throw new ArgumentNullException("user"); if ((HttpContext.Current.Session["User"] as UserData) == null) throw new InvalidOperationException("The connecting user isn't logged on."); // We'll do a simple replace of all the user data. Note that we don't expect the user's // name or email address to have changed. HttpContext.Current.Session["User"] = user; }
public static UserData DeserializeOAuth2ServerResponse(String jsonContent) { if (String.IsNullOrWhiteSpace(jsonContent)) return null; try { // Parse the JSON. var jObj = JObject.Parse(jsonContent); // Create UserData object, and populate the properties. var user = new UserData() { AccessToken = (String)jObj["access_token"], AccessTokenIssuedAt = DateTime.UtcNow, ExpiresIn = (Int32)jObj["expires_in"], RefreshToken = (String)jObj["refresh_token"], UserIdentifier = (String)jObj["user_id"], UserName = (String)jObj["user_name"] }; // Basic validation. if (String.IsNullOrWhiteSpace(user.AccessToken)) return null; if (user.ExpiresIn <= 0) return null; if (String.IsNullOrWhiteSpace(user.RefreshToken)) return null; if (String.IsNullOrWhiteSpace(user.UserIdentifier)) return null; if (user.UserName == null) return null; // Return. return user; } // Not sure what exceptions Newtonsoft.Json can throw, so will have to catch any/all. catch (Exception) { return null; } }
// Should be called when the connecting user logs on. public void OnLoggedOn(UserData user) { if (user == null) throw new ArgumentNullException("user"); HttpContext.Current.Session["User"] = user; }