/// <summary> /// Store OAuth 2.0 credentials in the application's database. /// </summary> /// <param name="userId">User's ID.</param> /// <param name="credentials">The OAuth 2.0 credentials to store.</param> static void StoreCredentials(String userId, IAuthorizationState credentials) { StoredCredentials sc = new StoredCredentials() { UserId = userId, AccessToken = credentials.AccessToken, RefreshToken = credentials.RefreshToken }; string json = Newtonsoft.Json.JsonConvert.SerializeObject(sc); System.IO.File.WriteAllText("token.google.drive." + userId + ".txt", json); //StoredCredentialsDBContext db = new StoredCredentialsDBContext(); //StoredCredentials sc = db.StoredCredentialSet.FirstOrDefault(x => x.UserId == userId); //if (sc != null) //{ // sc.AccessToken = credentials.AccessToken; // sc.RefreshToken = credentials.RefreshToken; //} //else //{ // db.StoredCredentialSet.Add(new StoredCredentials { UserId = userId, AccessToken = credentials.AccessToken, RefreshToken = credentials.RefreshToken }); //} //db.SaveChanges(); }
/// <summary> /// Retrieved stored credentials for the provided user ID. /// </summary> /// <param name="userId">User's ID.</param> /// <returns>Stored GoogleAccessProtectedResource if found, null otherwise.</returns> static IAuthorizationState GetStoredCredentials(String userId) { //StoredCredentialsDBContext db = new StoredCredentialsDBContext(); //StoredCredentials sc = db.StoredCredentialSet.FirstOrDefault(x => x.UserId == userId); //if (sc != null) //{ // return new AuthorizationState() { AccessToken = sc.AccessToken, RefreshToken = sc.RefreshToken }; //} if (System.IO.File.Exists("token.google.drive." + userId + ".txt")) { string json = System.IO.File.ReadAllText(userId + ".txt"); StoredCredentials sc = Newtonsoft.Json.JsonConvert.DeserializeObject <StoredCredentials>(json); if (sc != null) { return new AuthorizationState() { AccessToken = sc.AccessToken, RefreshToken = sc.RefreshToken } } ; } return(null); }