Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Graph API Scripted Authentication

How does one authenticate as a user without any direct user interaction?


Otherwise i found a workaround with client credential flow in this example : https://github.com/microsoftgraph/console-csharp-snippets-sample but if i try to implement this code in an c# Asp.net mav applcition or a windows forms application i cant get an application token. If i debug the app it stuck at waiting for token but it doesn't throw an error (virus protection already deactivated). Does anyone have a idea for the main problem or my workaround?

This is the Code for my workaround trying to get a token, but stuck on daemonClient.AcquireTokenForClientAsync.

   public async Task<Users> GetUser(string Username)
    {
        MSALCache appTokenCache = new MSALCache(clientId);

        ClientCredential clientdummy = new ClientCredential(clientSecret);
        
        ConfidentialClientApplication daemonClient = new ConfidentialClientApplication(clientId, string.Format(AuthorityFormat, tenantId), redirectUri,
                                                            clientdummy, null, null);

        authenticate(daemonClient).Wait();

        string token = authResult.AccessToken;

        client = GetAuthenticatedClientForApp(token);

        IGraphServiceUsersCollectionPage users = client.Users.Request().GetAsync().Result;
    }

    private async Task<AuthenticationResult> authenticate(ConfidentialClientApplication daemonClient)
    {
        authResult = await daemonClient.AcquireTokenForClientAsync(new[] { MSGraphScope });
        return authResult;
    }
like image 511
Evi Avatar asked Oct 20 '25 14:10

Evi


1 Answers

Found Workaround Solution: getting a Token via REST API. Here I can get an User token or an Client token to access to graph api:

 var client = new RestClient("https://login.microsoftonline.com/" + domainname);
 var request = new RestRequest("/oauth2/token", Method.POST);   request.AddBody("grant_type", "client_credentials");
        request.AddParameter("client_id", clientId);
        request.AddParameter("client_secret", clientSecret);
        request.AddParameter("Resource", "https://graph.microsoft.com");
        request.AddParameter("scope", "[scopes]"); 
        IRestResponse response = client.Execute(request);
        //contains the token 
        var content = response.Content;
like image 79
Evi Avatar answered Oct 22 '25 04:10

Evi