예제 #1
0
        /// <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
                });
            }
            return(null);
        }
예제 #2
0
        /// <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)
        {
            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();
        }