Esempio n. 1
0
        public static OneDriveAccessDetails GetUsersOneDriveAccessDetails(string userEmail)
        {
            try
            {
                // Get the user's config, which contains the refresh token
                // and the OneDrive resource ID
                Storage.AppConfig appConfig = Storage.AppConfigCache.GetUserConfig(userEmail);

                // Request authorization for OneDrive
                ClientCredential      credential  = new ClientCredential(ClientId, ClientSecret);
                string                authority   = string.Format(CultureInfo.InvariantCulture, OAuthUrl, "common");
                AuthenticationContext authContext = new AuthenticationContext(authority);
                AuthenticationResult  result      = authContext.AcquireTokenByRefreshToken(
                    appConfig.RefreshToken, ClientId, credential, appConfig.OneDriveResourceId);

                // Update refresh token
                appConfig.RefreshToken = result.RefreshToken;
                Storage.AppConfigCache.AddUserConfig(userEmail, appConfig);

                return(new OneDriveAccessDetails()
                {
                    ApiEndpoint = appConfig.OneDriveApiEndpoint,
                    AccessToken = result.AccessToken
                });
            }
            catch (ActiveDirectoryAuthenticationException)
            {
                return(null);
            }
        }
Esempio n. 2
0
        public bool IsConsentInPlace(AuthorizationRequest request)
        {
            Storage.AppConfig config = Storage.AppConfigCache.GetUserConfig(request.UserEmail);

            // If we have a refresh token for this user, we already have consent
            if (config != null && !string.IsNullOrEmpty(config.RefreshToken))
            {
                return(true);
            }
            return(false);
        }
Esempio n. 3
0
        public string CompleteOAuthFlow(AuthorizationParameters parameters)
        {
            // Look up the email from the guid/user map.
            string userEmail = Storage.AppConfigCache.GetUserFromStateGuid(parameters.State);

            if (string.IsNullOrEmpty(userEmail))
            {
                // Per the Azure docs, the response from the auth code request has
                // to include the value of the state parameter passed in the request.
                // If it is not the same, then you should not accept the response.
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.OK,
                                                                            "Unknown state returned in OAuth flow."));
            }

            try
            {
                // Get authorized for the discovery service
                ClientCredential      credential  = new ClientCredential(ClientId, ClientSecret);
                string                authority   = string.Format(CultureInfo.InvariantCulture, OAuthUrl, "common");
                AuthenticationContext authContext = new AuthenticationContext(authority);
                AuthenticationResult  result      = authContext.AcquireTokenByAuthorizationCode(
                    parameters.Code, new Uri(RedirectUrl.GetLeftPart(UriPartial.Path)), credential, DiscoveryResource);

                // Cache the refresh token
                Storage.AppConfig appConfig = new Storage.AppConfig();
                appConfig.RefreshToken = result.RefreshToken;

                // Use the access token to get the user's OneDrive URL
                OneDriveServiceInfo serviceInfo = DiscoverServiceInfo(result.AccessToken);
                appConfig.OneDriveResourceId  = serviceInfo.ResourceId;
                appConfig.OneDriveApiEndpoint = serviceInfo.Endpoint;

                // Save the user's configuration in our confic cache
                Storage.AppConfigCache.AddUserConfig(userEmail, appConfig);
                return("OAuth succeeded. Please close this window to continue.");
            }
            catch (ActiveDirectoryAuthenticationException ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.OK,
                                                                            "OAuth failed. " + ex.ToString()));
            }
        }