Exemplo n.º 1
0
        public async Task <bool> AuthenticateNew(CancellationToken cs)
        {
            //msaAuthenticationProvider = new MsaAuthenticationProvider(MicrosoftSecret.ClientId, MicrosoftSecret.ClientSecret, "http://localhost:8000/callback.html", Scopes, null, new CredentialsVault(this, null));
            //await msaAuthenticationProvider.AuthenticateUserAsync().ConfigureAwait(false);

            //oneDriveClient = new OneDriveClient("https://api.onedrive.com/v1.0", msaAuthenticationProvider);

            //return msaAuthenticationProvider.IsAuthenticated;


            Task authTask;
            var  adalAuthProvider = new AdalAuthenticationProvider(
                "053488e0-5bf2-467b-8866-45d00c5b187b",
                "https://boschsg.onmicrosoft.com/OneDriveApi2");

            //oneDriveClient = new OneDriveClient("https://boschsg-my.sharepoint.com" + "/_api/v2.0", adalAuthProvider);
            oneDriveClient = new OneDriveClient("https://boschsg-my.sharepoint.com" + "/_api/v2.0", adalAuthProvider);

            authTask = adalAuthProvider.AuthenticateUserAsync("https://boschsg-my.sharepoint.com");
            try
            {
                await authTask;
            }
            catch (ServiceException exception)
            {
                Console.WriteLine(exception.Error);
            }

            return(true);
        }
Exemplo n.º 2
0
        public override void Setup()
        {
            base.Setup();

            this.adalServiceInfo = new AdalServiceInfo();
            this.adalServiceInfo.CopyFrom(this.serviceInfo);

            this.authenticationProvider = new AdalAuthenticationProvider(this.adalServiceInfo);
        }
        private async void InitializeClient(ClientType clientType, RoutedEventArgs e)
        {
            var app = (App)Application.Current;

            if (app.OneDriveClient == null)
            {
                Task authTask;

                if (clientType == ClientType.Business)
                {
                    var adalAuthProvider = new AdalAuthenticationProvider(
                        this.oneDriveForBusinessClientId,
                        this.oneDriveForBusinessReturnUrl);
                    authTask           = adalAuthProvider.AuthenticateUserAsync(this.oneDriveForBusinessBaseUrl);
                    app.OneDriveClient = new OneDriveClient(this.oneDriveForBusinessBaseUrl + "/_api/v2.0", adalAuthProvider);
                    app.AuthProvider   = adalAuthProvider;
                }
                else if (clientType == ClientType.ConsumerUwp)
                {
                    var onlineIdAuthProvider = new OnlineIdAuthenticationProvider(
                        this.scopes);
                    authTask           = onlineIdAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
                    app.OneDriveClient = new OneDriveClient(this.oneDriveConsumerBaseUrl, onlineIdAuthProvider);
                    app.AuthProvider   = onlineIdAuthProvider;
                }
                else
                {
                    var msaAuthProvider = new MsaAuthenticationProvider(
                        this.oneDriveConsumerClientId,
                        this.oneDriveConsumerReturnUrl,
                        this.scopes,
                        new CredentialVault(this.oneDriveConsumerClientId));
                    authTask           = msaAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
                    app.OneDriveClient = new OneDriveClient(this.oneDriveConsumerBaseUrl, msaAuthProvider);
                    app.AuthProvider   = msaAuthProvider;
                }

                try
                {
                    await authTask;
                    app.NavigationStack.Add(new ItemModel(new Item()));
                    this.Frame.Navigate(typeof(MainPage), e);
                }
                catch (ServiceException exception)
                {
                    // Swallow the auth exception but write message for debugging.
                    Debug.WriteLine(exception.Error.Message);
                }
            }
            else
            {
                this.Frame.Navigate(typeof(MainPage), e);
            }
        }
        public void Setup()
        {
            this.credentialCache     = new MockAdalCredentialCache();
            this.httpResponseMessage = new HttpResponseMessage();
            this.serializer          = new Serializer();
            this.httpProvider        = new MockHttpProvider(this.httpResponseMessage, this.serializer);

            this.serviceInfo = new ActiveDirectoryServiceInfo
            {
                AppId = "12345",
                AuthenticationServiceUrl = "https://login.live.com/authenticate",
                CredentialCache          = this.credentialCache.Object,
                HttpProvider             = this.httpProvider.Object,
                ReturnUrl       = "https://login.live.com/return",
                SignOutUrl      = "https://login.live.com/signout",
                TokenServiceUrl = "https://login.live.com/token"
            };

            this.authenticationProvider = new AdalAuthenticationProvider(this.serviceInfo);
        }
        public async Task <BusinessServiceInformation> AuthenticateWithDiscoveryServiceAsync(
            DiscoveryServiceResponse discoveryServiceResponse = null,
            string refreshToken = null)
        {
            bool refresh = refreshToken != null;

            var mockAuthenticationResult = new MockAuthenticationResult();

            mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token");
            mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns((string)null);
            mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow.AddHours(1));

            var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper();

            if (refresh)
            {
                mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenByRefreshTokenAsync(
                                                           It.Is <string>(token => token.Equals(refreshToken)),
                                                           It.Is <string>(clientId => clientId.Equals(AuthenticationTestBase.ClientId)),
                                                           It.Is <string>(resource => resource.Equals(OAuthConstants.ActiveDirectoryDiscoveryResource))))
                .Returns(Task.FromResult(mockAuthenticationResult.Object));
            }
            else
            {
                mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync(
                                                           It.Is <string>(resource => resource.Equals(OAuthConstants.ActiveDirectoryDiscoveryResource)),
                                                           It.Is <string>(clientId => clientId.Equals(AuthenticationTestBase.ClientId)),
                                                           UserIdentifier.AnyUser))
                .Returns(Task.FromResult(mockAuthenticationResult.Object));
            }

            var authenticationProvider = new AdalAuthenticationProvider(
                AuthenticationTestBase.ClientId,
                AuthenticationTestBase.ReturnUrl,
                mockAuthenticationContextWrapper.Object);

            var discoveryServiceHelper = new DiscoveryServiceHelper(authenticationProvider);

            if (discoveryServiceResponse == null)
            {
                discoveryServiceResponse = new DiscoveryServiceResponse
                {
                    Value = new List <DiscoveryService>
                    {
                        new DiscoveryService
                        {
                            Capability         = "MyFiles",
                            ServiceApiVersion  = "v2.0",
                            ServiceEndpointUri = AuthenticationTestBase.ServiceEndpointUrl,
                            ServiceResourceId  = AuthenticationTestBase.ServiceResourceId,
                        }
                    }
                };
            }

            var requestBodyString = this.serializer.SerializeObject(discoveryServiceResponse);

            BusinessServiceInformation businessServiceInformation = null;

            using (var stringContent = new StringContent(requestBodyString))
            {
                this.httpResponseMessage.Content = stringContent;

                if (refresh)
                {
                    businessServiceInformation = await discoveryServiceHelper.DiscoverFilesEndpointInformationForUserWithRefreshTokenAsync(
                        refreshToken,
                        httpProvider : this.httpProvider.Object);
                }
                else
                {
                    businessServiceInformation = await discoveryServiceHelper.DiscoverFilesEndpointInformationForUserAsync(httpProvider : this.httpProvider.Object);
                }
            }

            return(businessServiceInformation);
        }