Acces MsGraph by Hybrid Azure AD Joined
![](https://techprofile.blob.core.windows.net/images/goG1iqgfyUC8pnpDqzb4Tg.png?8D8982)
Hi,
I have a WPF application (target the old .NET Framework runtime) that request to MsGraph with the NuGet package Micrsoft.Graph. That work perfectly.
We try to adapt the WPF application to run out of our enterprise domain.
We have configured Azure Conditionnal Access and all computers are configured to be Hybrid Azure AD Joined. We have checked with dsregcmd /status
.
But out of our domain, the application fail to request to MsGraph. I reproduce with this minimal example that retrieve the followed SharePoint site (work in domain/not work out of domain) :
static void Main(string[] args)
{
Console.WriteLine("Try get token");
var auth = new AuthenticationContext($"https://login.windows.net/common");
var token = auth.AcquireTokenAsync(
"https://MyCompany.onmicrosoft.com/MyApplication",
"ClientGuid",
new UserCredential()
).Result.AccessToken;
Console.WriteLine("ToKen : " + token);
var graphService = new BetaGraphServiceClientO365(
new AzureOnBehalfAccessToken(
new AuthenticationAzureOptions {
AadInstance = "https://login.microsoftonline.com/",
AppIdUri = "https://MyCompany.onmicrosoft.com/MyApp",
ClientId = "ClientGuid",
Tenant = "TenandGuid",
ClientSecret = "ClientSecret"
},
new Microsoft.Extensions.Caching.Distributed.MemoryDistributedCache(new OptionCaching()),
4269, //User id
token
),
"https://graph.microsoft.com/"
);
var sites = graphService.Me.FollowedSites.Request().GetAsync().Result;
Console.WriteLine();
Console.WriteLine("Followed sites : " + sites.Count);
foreach(var site in sites)
{
Console.WriteLine(site.DisplayName);
}
}
This fail on AcquireTokenAsync
with the error :
Exception non gérée : System.AggregateException: Une ou plusieurs erreurs se sont produites. --->
Microsoft.IdentityModel.Clients.ActiveDirectory.AdalClaimChallengeException: AADSTS53001: Device is not in required device state: domain_joined.
Conditional Access policy requires a domain joined device, and the device is not domain joined.
Trace ID: 08692b13-4a26-4668-9ab0-c361cfa02a00
Correlation ID: bc0ebc0b-22cf-435e-b9c5-b3ab34b3905a
Timestamp: 2021-04-02 07:16:47Z --->
Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException: Response status code does not indicate success: 400 (BadRequest).
à Microsoft.Identity.Core.OAuth2.OAuthClient.<GetResponseAsync>d__18`1.MoveNext()
--- Fin de la trace de la pile d'exception interne ---
à Microsoft.Identity.Core.OAuth2.OAuthClient.<GetResponseAsync>d__18`1.MoveNext()
--- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à Microsoft.Identity.Core.OAuth2.OAuthClient.<HandleDeviceAuthChallengeAsync>d__21`1.MoveNext()
--- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à Microsoft.Identity.Core.OAuth2.OAuthClient.<GetResponseAsync>d__18`1.MoveNext()
--- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à Microsoft.Identity.Core.OAuth2.OAuthClient.<GetResponseAsync>d__17`1.MoveNext()
--- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows.AcquireTokenHandlerBase.<SendHttpMessageAsync>d__75.MoveNext()
--- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows.AcquireTokenHandlerBase.<SendTokenRequestAsync>d__72.MoveNext()
--- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows.AcquireTokenHandlerBase.<CheckAndAcquireTokenUsingBrokerAsync>d__62.MoveNext()
--- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows.AcquireTokenHandlerBase.<RunAsync>d__60.MoveNext()
--- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.<AcquireTokenCommonAsync>d__40.MoveNext()
--- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions.<AcquireTokenAsync>d__0.MoveNext()
--- Fin de la trace de la pile d'exception interne ---
à System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
à MsGraphDemo.Program.Main(String[] args) dans D:\users\cevernou\Projects\Vrac\MsGraphDemo\Program.cs:ligne 14
How request to MsGraph from a Hybrid Azure AD Joined computer?