Search code examples
c#uwpcalendarwin-universal-appoutlook-restapi

UWP Microsoft.Identity.Client pass login without promt


I'm accessing an Outlook calendar with the Microsoft Graph API. In my UWP App I'm using the Microsoft.Identity.Client, which is available on Nuget. This works without issues, but for the first time I want to get a users calendar, I have to sign-in. Here's my code for authenticating / getting a token

private async Task<string> GetTokenForUserAsync()
{
    string tokenForUser = null;
    string[] Scopes = { "https://graph.microsoft.com/Calendars.Read" };

    PublicClientApplication identityClient = new PublicClientApplication(clientId);
    AuthenticationResult authResult;
    IEnumerable<IUser> users = identityClient.Users;

    if (users.Count() > 0)
    {
        try
        {
            authResult = await identityClient.AcquireTokenSilentAsync(Scopes, users.First());
            tokenForUser = authResult.AccessToken;
        }
        catch
        {
            tokenForUser = null;
        }
    }
    else
    {
        try
        {
            authResult = await identityClient.AcquireTokenAsync(Scopes);
            tokenForUser = authResult.AccessToken;
        }
        catch
        {
            tokenForUser = null;
        }
    }

    return tokenForUser;
}

When calling this Task for the first time, I have to log in with my Outlook credentials inside some sort of WebView which gets opened. After the first request, this is not needed anymore, because identityClient.Users does contain my logged in user. Now what I try to achieve is that I can hardcode my login and pass it to the authentication. But the only thing what I have found is the ability to provide the login username (Outlook mail address) with the AcquireTokenAsync() overload

authResult = await identityClient.AcquireTokenAsync(Scopes, "[email protected]");

But there is no overload inside this method to provide the password. So is there any other option, to pass the password to this call? The main reason why I'm using the REST API is because this app is running on Windows 10 IoT Core and there is no AppointmentStore (local calendar) available.


Solution

  • After trying different solutions, which didn't provide me what i'm looking for, I decided to go another way. Now for reading a calendar, I simply use subscreibed ics / ical files, which provides nearly realtime access to a calendar without authorization.