Beispiel #1
0
        /// <summary>
        /// Gets a valid authentication token. Also refreshes the access token if it has expired.
        /// </summary>
        /// <remarks>
        /// Used by the API request generators before making calls to the OneNote APIs.
        /// </remarks>
        /// <returns>valid authentication token</returns>
        internal static async Task <string> GetAuthToken()
        {
            if (String.IsNullOrWhiteSpace(_accessToken))
            {
                try
                {
                    var serviceTicketRequest = new OnlineIdServiceTicketRequest(Scopes, "DELEGATION");
                    var result =
                        await Authenticator.AuthenticateUserAsync(new[] { serviceTicketRequest }, CredentialPromptType.PromptIfNeeded);

                    if (result.Tickets[0] != null)
                    {
                        _accessToken           = result.Tickets[0].Value;
                        _accessTokenExpiration = DateTimeOffset.UtcNow.AddMinutes(AccessTokenApproxExpiresInMinutes);
                    }
                }
                catch (Exception ex)
                {
                    // Authentication failed
                    if (Debugger.IsAttached)
                    {
                        Debugger.Break();
                    }
                }
            }
            await RefreshAuthTokenIfNeeded();

            return(_accessToken);
        }
Beispiel #2
0
        public async Task <bool> EnsureUserIdentityAsync(
            CancellationToken cancelToken,
            string msaAuthPolicy)
        {
            OnlineIdServiceTicketRequest[] tickets = new OnlineIdServiceTicketRequest[]
            {
                new OnlineIdServiceTicketRequest(
                    LiveIdHostName,
                    msaAuthPolicy)
            };

            try
            {
                var onlineIdAuthenticator = new OnlineIdAuthenticator();
                m_userIdentity = await onlineIdAuthenticator.AuthenticateUserAsync(tickets, CredentialPromptType.PromptIfNeeded);

                return(true);
            }
            catch (TaskCanceledException) {
                throw;
            }
            catch (Exception)
            {
                return(false);;
            }
        }
Beispiel #3
0
        private async void SignInButton_Click(object sender, RoutedEventArgs e)
        {
            var targetArray = new List <OnlineIdServiceTicketRequest>();

            targetArray.Add(new OnlineIdServiceTicketRequest("jsonwebtokensample.com", "JWT"));

            SignInButton.Visibility = Visibility.Collapsed;

            try
            {
                var result = await _authenticator.AuthenticateUserAsync(targetArray, PromptType);

                if (result.Tickets[0].Value != string.Empty)
                {
                    DebugPrint("Signed in.");
                    NeedsToGetTicket = false;

                    Token = result.Tickets[0].Value;
                }
                else
                {
                    // errors are to be handled here.
                    DebugPrint("Unable to get the ticket. Error: " + result.Tickets[0].ErrorCode.ToString());
                }
            }
            catch (System.Exception ex)
            {
                // errors are to be handled here.
                DebugPrint("Unable to get the ticket. Exception: " + ex.Message);
            }

            NeedsToGetTicket        = Token == null || Token.Length == 0;
            SignInButton.Visibility = NeedsToGetTicket ? Visibility.Visible : Visibility.Collapsed;
        }
        public async Task <bool> AuthenticateAsync(params string[] services)
        {
            List <OnlineIdServiceTicketRequest> targetArray = new List <OnlineIdServiceTicketRequest>();

            targetArray.Add(new OnlineIdServiceTicketRequest(string.Join(" ", services), POLICY_DELEGATION));

            try
            {
                UserIdentity = await _authenticator.AuthenticateUserAsync(targetArray, CredentialPromptType.PromptIfNeeded);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Beispiel #5
0
        public async Task <AuthResult> AuthAsync(string startUrl, string endUrlPrefix)
        {
            AuthResult result = new AuthResult(WebAuthenticationStatus.UserCancel);
            Uri        start  = null;

            OnlineIdServiceTicketRequest[] tickets = new OnlineIdServiceTicketRequest[]
            {
                new OnlineIdServiceTicketRequest(
                    new Uri(startUrl).Host,
                    String.IsNullOrEmpty(LiveIdAuthPolicy)
                        ? ServiceDefinition.DefaultLiveIdAuthPolicy
                        : LiveIdAuthPolicy)
            };

            try
            {
                var          onlineIdAuthenticator = new OnlineIdAuthenticator();
                UserIdentity useridentity          =
                    await onlineIdAuthenticator.AuthenticateUserAsync(tickets, CredentialPromptType.PromptIfNeeded);

                if (useridentity != null && useridentity.Tickets != null && useridentity.Tickets.Count > 0)
                {
                    OnlineIdServiceTicket ticket = useridentity.Tickets.First();
                    start = new Uri(startUrl + WebUtility.UrlEncode("&" + ticket.Value) + "&mobile=true");
                }
            }
            catch (TaskCanceledException)
            {
                result.Status = WebAuthenticationStatus.UserCancel;
            }
            catch
            {
                start = new Uri(startUrl + "&mobile=true");
            }

            if (start != null)
            {
                WebAuthenticationResult webAuthResult = (await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, start, new Uri(endUrlPrefix)));
                result = new AuthResult(webAuthResult);
            }

            return(result);
        }
Beispiel #6
0
        private async void SignInButton_Click(object sender, RoutedEventArgs e)
        {
            var targetArray = new List <OnlineIdServiceTicketRequest>();

            targetArray.Add(new OnlineIdServiceTicketRequest("wl.basic wl.contacts_photos office.onenote_create", "DELEGATION"));

            AccessToken      = null;
            NeedsToGetTicket = true;

            DebugPrint("Signing in...");

            try
            {
                var result = await _authenticator.AuthenticateUserAsync(targetArray, PromptType);

                if (result.Tickets[0].Value != string.Empty)
                {
                    DebugPrint("Signed in.");

                    NeedsToGetTicket = false;
                    AccessToken      = result.Tickets[0].Value;
                }
                else
                {
                    // errors are to be handled here.
                    DebugPrint("Unable to get the ticket. Error: " + result.Tickets[0].ErrorCode.ToString());
                }
            }
            catch (System.OperationCanceledException)
            {
                // errors are to be handled here.
                DebugPrint("Operation canceled.");
            }
            catch (System.Exception ex)
            {
                // errors are to be handled here.
                DebugPrint("Unable to get the ticket. Exception: " + ex.Message);
            }

            SignInButton.Visibility     = NeedsToGetTicket ? Visibility.Visible : Visibility.Collapsed;
            GoNextStackpanel.Visibility = NeedsToGetTicket ? Visibility.Collapsed : Visibility.Visible;
        }