wpfoauth-2.0microsoft-graph-apiadalonedrive

Graph SDK OAUTH ADAL client credentials for reuse? later


So we have an application that allows you to create a xml file that runs the app again at a later stage (which may or may not have a user in attendance). Files are stored on the user cloud drive platform of choice. So the process is

Workflow 1

Authenticate to cloud with User 1 details/input Select files to Download and use Save and encrypt file metadata and Refresh token to xml file. (app workflow 1)

Workflow 2 ( can be repeated multiple thousands of times) Send xml file to another pc with User 2. (either by email or remotely through a console that pc then runs under user 2

start app App authenticates automatically using refresh token saved in xml file with no user input (as there is a very high chance of the user who created workflow 1 not being in the same city as where workflow 2 is running Downloads files applies files (app workflow 2) PROBLEM all other platforms we cater for (Dropbox and google and onedrive) gives us access to the refresh token and allow us to authenticate with it again , however the onedrive for business (graph sdks) give us a Token cache which is session based?

Questions

So I need to know how I can get the refresh token from the Token Cache so we can reuse it at a later stage. (yes I'm aware that it will expire after 6 months which is acceptable) .

When i have the refresh token how do initiate a call to refresh the token

Further note - I have managed to handle all platforms before Within silverlight (where the sdks are not supported) through directly calling the rest api calls but we are converting our solution to WPF and would want to use the sdks

thanks


Solution

  • OK so the answer is taking the tokencache from the PublicIdentityApp and serializing as below.

       var tokenCache = _app.UserTokenCache;
       var tokenBytes = tokenCache.Serialize();
       var tokenString = Convert.ToBase64String(tokenBytes);
    

    And deserializing it later.

    _app = new Microsoft.Identity.Client.PublicClientApplication(ClientID);
    var array = Convert.FromBase64String(bytestring);
    _app.UserTokenCache.Deserialize(array);
    authHelper = new AuthenticationHelper(_app);
    var authorise = await authHelper.GetTokenForUserAsync()