public async Task MsAppRedirectUriTest() { Sts sts = new AadSts(); AuthenticationContext context = await AuthenticationContext.CreateAsync(sts.Authority); try { UserIdentifierType t = UserIdentifierType.RequiredDisplayableId; context.AcquireTokenAndContinue(sts.ValidResource, sts.ValidClientId, new Uri("ms-app://test/"), null); Verify.Fail("Argument exception expected"); } catch (AdalException ex) { Verify.AreEqual(ex.ErrorCode, Sts.AuthenticationUiFailedError); Verify.IsTrue(ex.InnerException is ArgumentException); } try { WebAuthenticationBroker.GetCurrentApplicationCallbackUri(); Verify.Fail("Exception expected"); } catch (Exception ex) { Verify.IsTrue(ex.Message.Contains("hostname")); } try { context.AcquireTokenAndContinue(sts.ValidResource, sts.ValidClientId, null, null); Verify.Fail("Exception expected"); } catch (AdalException ex) { Verify.AreEqual(ex.ErrorCode, "need_to_set_callback_uri_as_local_setting"); } try { // Incorrect ms-app ApplicationData.Current.LocalSettings.Values["CurrentApplicationCallbackUri"] = "ms-app://s-1-15-2-2097830667-3131301884-2920402518-3338703368-1480782779-4157212157-3811015497/"; context.AcquireTokenAndContinue(sts.ValidResource, sts.ValidClientId, null, null); Verify.Fail("Exception expected"); } catch (AdalException ex) { Verify.AreEqual(ex.ErrorCode, Sts.AuthenticationUiFailedError); } }
protected override async Task <AuthenticationResult> AcquireToken(string serverUrl, string clientId, string redirectUrl) { await InitializeAuthentication(); acquireTokenTcs = new TaskCompletionSource <AuthenticationResult>(); authContext.AcquireTokenAndContinue(serverUrl, clientId, new Uri(redirectUrl), p => { }); return(await acquireTokenTcs.Task); }
// fetch the user's To Do list from the service. If no tokens are present in the cache, trigger the authentication experience before performing the call private async void RefreshAppBarButton_Click(object sender, RoutedEventArgs e) { // Try to get a token without triggering any user prompt. // ADAL will check whether the requested token is in the cache or can be obtained without user itneraction (e.g. via a refresh token). AuthenticationResult result = await authContext.AcquireTokenSilentAsync(todoListResourceId, clientId); if (result != null && result.Status == AuthenticationStatus.Success) { // A token was successfully retrieved. Get the To Do list for the current user GetTodoList(result); } else { // Acquiring a token without user interaction was not possible. // Trigger an authentication experience and specify that once a token has been obtained the GetTodoList method should be called authContext.AcquireTokenAndContinue(todoListResourceId, clientId, redirectURI, GetTodoList); } }
public static async void BeginAuthentication() { //First, look for the authority used during the last authentication. //If that value is not populated, use CommonAuthority. string authority = null; if (String.IsNullOrEmpty(LastAuthority)) { authority = CommonAuthority; } else { authority = LastAuthority; } _authenticationContext = await AuthenticationContext.CreateAsync(authority); _authenticationContext.AcquireTokenAndContinue(DiscoveryResourceId, ClientID, _returnUri, null); }
public static async System.Threading.Tasks.Task GetTokenSilent() { if (String.IsNullOrEmpty(CRMHelper.AuthorityUrl)) { await CRMHelper.DiscoveryAuthority(); } // If authContext is null, then generate it. if (authContext == null) #if WINDOWS_PHONE_APP { // ADAL for Windows Phone 8.1 builds AuthenticationContext instances throuhg a factory, which performs authority validation at creation time authContext = AuthenticationContext.CreateAsync(CRMHelper.AuthorityUrl).GetResults(); } AuthenticationResult result = await authContext.AcquireTokenSilentAsync(CRMHelper.ResourceName, CRMHelper.ClientId); #else { authContext = new AuthenticationContext(CRMHelper.AuthorityUrl, false); } AuthenticationResult result = await authContext.AcquireTokenAsync(CRMHelper.ResourceName, CRMHelper.ClientId); #endif if (result != null && result.Status == AuthenticationStatus.Success) { // A token was successfully retrieved. Then store it. StoreToken(result); } else { #if WINDOWS_PHONE_APP // Clear the AccessToken first so that any Service Calls waits until it's filled. proxy.AccessToken = ""; // In case credential was wrong, clear the token cache first. authContext.TokenCache.Clear(); // Acquiring a token without user interaction was not possible. // Trigger an authentication experience and specify that once a token has been obtained the StoreToken method should be called. authContext.AcquireTokenAndContinue(CRMHelper.ResourceName, CRMHelper.ClientId, new Uri(CRMHelper.RedirectUri), StoreToken); #else DisplayErrorWhenAcquireTokenFails(result); #endif } }
/// <summary> /// This method try to obtain AccessToken by using OAuth2 authentication agianst Microsoft Azure AD. /// </summary> static public async Task GetTokenSilent() { // Before create AuthenticationContext, check if Authority(OAuthUrl) is available. if (String.IsNullOrEmpty(CRMHelper.OAuthUrl)) { bool success = true; try { await CRMHelper.DiscoveryAuthority(); } catch (Exception ex) { // If failed to retireve OAuthUrl, then make success as false success = false; } // If failed to retrieve OAuthUrl, chances are user mistype ServerUrl. if (!success) { MessageDialog dialog = new MessageDialog("OAuth Url retrieve failed. Please check Service URL again."); await dialog.ShowAsync(); return; } } if (CRMHelper.SignOut) { if (authContext != null) { authContext.TokenCache.Clear(); } CRMHelper.SignOut = false; } // Create AuthenticationContext by using OAuthUrl. if (authContext == null) { authContext = await AuthenticationContext.CreateAsync(CRMHelper.OAuthUrl); } // Try to acquire token without prompting user first. AuthenticationResult result = await authContext.AcquireTokenSilentAsync(CRMHelper.ResourceName, CRMHelper.ClientId); // Check the result. if (result != null && result.Status == AuthenticationStatus.Success) { // A token was successfully retrieved. Then store it. StoreToken(result); } // If failed to obtain token without prompting, then prompt user for credentials else { // Clear AccessToken CRMHelper._proxy.AccessToken = ""; // In case credential was wrong, clear the token cache first. authContext.TokenCache.Clear(); // Acquiring a token without user interaction was not possible. // Trigger an authentication experience and specify that once a token has been obtained the StoreToken method should be called. authContext.AcquireTokenAndContinue(CRMHelper.ResourceName, CRMHelper.ClientId, new Uri(CRMHelper.RedirectUri), StoreToken); } }