I have a console application registered in Azure AD that connects to CRM Online (using these steps ). It requests a web API.
The application should start without user interaction ... but, unfortunately, the AcquireTokenSilentAsync call always fails and only AcquireTokenAsync works. This will result in a user login dialog box that does not meet the user's requirements!
Is there a way to prevent this invitation , either by storing the login somewhere on the client machine (which has not worked yet), or perhaps by using a certificate (but how do you do this?) Or something else?
I am using the ADAL release for .NET v3.10.305110106. The following code is used for authentication:
private static async Task PerformOnlineAuthentication() { _authInfo = new AuthInfo(); // This is just a simple class of parameters Console.Write("URL (include /api/data/v8.x): "); var url = Console.ReadLine(); BaseUri = new Uri(url); var absoluteUri = BaseUri.AbsoluteUri; _authInfo.Resource = absoluteUri; Console.Write("ClientId: "); var clientId = Console.ReadLine(); _authInfo.ClientId = clientId; Console.Write("RedirectUri: "); var redirectUri = Console.ReadLine(); _authInfo.RedirectUri = new Uri(redirectUri); var authResourceUrl = new Uri($"{_authInfo.Resource}/api/data/"); var authenticationParameters = await AuthenticationParameters.CreateFromResourceUrlAsync(authResourceUrl); _authInfo.AuthorityUrl = authenticationParameters.Authority; _authInfo.Resource = authenticationParameters.Resource; _authInfo.Context = new AuthenticationContext(_authInfo.AuthorityUrl, false); } private static async Task RefreshAccessToken() { if (!IsCrmOnline()) return; Console.WriteLine($"Acquiring token from: {_authInfo.Resource}"); AuthenticationResult authResult; try { authResult = await _authInfo.Context.AcquireTokenSilentAsync(_authInfo.Resource, _authInfo.ClientId); } catch (AdalSilentTokenAcquisitionException astae) { Console.WriteLine(astae.Message); authResult = await _authInfo.Context.AcquireTokenAsync(_authInfo.Resource, _authInfo.ClientId, _authInfo.RedirectUri, new PlatformParameters(PromptBehavior.RefreshSession)); } HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken); }
source share