/// <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 and OneDrive 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); // TODO: catch exception when user says no 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; }
internal async Task<AccountSession> GetAccountSessionAsync() { try { var serviceTicketRequest = new OnlineIdServiceTicketRequest(string.Join(" ", this.ServiceInfo.Scopes), "DELEGATION"); var authenticationResponse = await this.authenticator.AuthenticateUserAsync(serviceTicketRequest); var ticket = authenticationResponse.Tickets.FirstOrDefault(); var accountSession = new AccountSession { AccessToken = ticket == null ? null : ticket.Value, AccountType = this.ServiceInfo.AccountType, CanSignOut = this.authenticator.CanSignOut, ClientId = this.authenticator.ApplicationId.ToString(), UserId = authenticationResponse.SafeCustomerId, }; return accountSession; } catch (Exception exception) { throw new OneDriveException(new Error { Code = OneDriveErrorCode.AuthenticationFailure.ToString(), Message = exception.Message }, exception); } }
internal async Task<AccountSession> GetAccountSessionAsync() { await this.SignOutAsync(); var serviceTicketRequest = new OnlineIdServiceTicketRequest(string.Join(" ", this.ServiceInfo.Scopes), "DELEGATION"); var authenticationResponse = await this.authenticator.AuthenticateUserAsync(serviceTicketRequest); var ticket = authenticationResponse.Tickets.FirstOrDefault(); var accountSession = new AccountSession { AccessToken = ticket == null ? null : ticket.Value, AccountType = this.ServiceInfo.AccountType, CanSignOut = this.authenticator.CanSignOut, ClientId = this.authenticator.ApplicationId.ToString(), UserId = authenticationResponse.SafeCustomerId, }; return accountSession; }
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; }
internal async Task<AccountSession> GetAccountSessionAsync() { try { var serviceTicketRequest = new OnlineIdServiceTicketRequest(string.Join(" ", this.ServiceInfo.Scopes), "DELEGATION"); var ticketRequests = new List<OnlineIdServiceTicketRequest>(); ticketRequests.Add(serviceTicketRequest); var authenticationResponse = await this.authenticator.AuthenticateUserAsync(ticketRequests, (Windows.Security.Authentication.OnlineId.CredentialPromptType) this.ServiceInfo.MicrosoftAccountPromptType); var ticket = authenticationResponse.Tickets.FirstOrDefault(); if (ticket == null || string.IsNullOrEmpty(ticket.Value)) { throw new OneDriveException( new Error { Code = OneDriveErrorCode.AuthenticationFailure.ToString(), Message = string.Format( "Failed to retrieve a valid authentication token from OnlineIdAuthenticator for user {0}.", authenticationResponse.SignInName) }); } var accountSession = new AccountSession { AccessToken = ticket == null ? null : ticket.Value, AccountType = this.ServiceInfo.AccountType, CanSignOut = this.authenticator.CanSignOut, ClientId = this.authenticator.ApplicationId.ToString(), ExpiresOnUtc = DateTimeOffset.UtcNow.AddMinutes(this.ticketExpirationTimeInMinutes), UserId = authenticationResponse.SafeCustomerId, }; return accountSession; } catch (Exception exception) { throw new OneDriveException(new Error { Code = OneDriveErrorCode.AuthenticationFailure.ToString(), Message = exception.Message }, exception); } }
private static async Task<MobileServiceUser> DoLoginAsync(CredentialPromptType promptType) { MobileServiceUser user = null; var authenticator = new OnlineIdAuthenticator(); var mobileServicesTicket = new OnlineIdServiceTicketRequest("wl.signin", "DELEGATION"); var ticketRequests = new List<OnlineIdServiceTicketRequest> {mobileServicesTicket}; var authResult = await authenticator.AuthenticateUserAsync(ticketRequests, promptType); if ((authResult.Tickets.Count == 1) && (authResult.Tickets[0].ErrorCode == 0)) { var accessToken = authResult.Tickets[0]; var payload = new JObject { ["access_token"] = accessToken.Value }; user = await MobileService.Client.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount, payload); LegacyUserId.Value = authResult.SafeCustomerId; } return user; }