KoenZomers / OneDriveAPI

API in .NET Framework 4.8.0, .NET Core 3.1 and .NET 6.0 to communicate with OneDrive Personal and OneDrive for Business
Eclipse Public License 1.0
109 stars 34 forks source link

Fix OneDriveGraphApi when use custom redirect url error. #20

Closed kgamecarter closed 4 years ago

kgamecarter commented 4 years ago

Fix issue #18

KoenZomers commented 4 years ago

Thanks for your contribution. I would like to test this first as it really should not be necessary to provide a client secret. Can you provide me with more detailed steps on how to reproduce the issue please?

kgamecarter commented 4 years ago

Create new Azure AD application. image Add custom redirect url. image

In ASP.NET Core MVC project.

private static OneDriveGraphApi oneDriveApi = new OneDriveGraphApi("4f205cff-965c-4f5e-b867-f5e27721c2db");

public async Task<IActionResult> AuthOneDrive()
{
    oneDriveApi.AuthenticationRedirectUrl = "https://localhost:44307/Home/ParseToken";
    return Redirect(oneDriveApi.GetAuthenticationUri().AbsoluteUri);
}

public async Task<IActionResult> ParseToken()
{
    var url = Microsoft.AspNetCore.Http.Extensions.UriHelper.GetDisplayUrl(Request);
    var authToken = oneDriveApi.GetAuthorizationTokenFromUrl(url);
    try
    {
        var token = await oneDriveApi.GetAccessToken();
        return RedirectToAction("Index");
    }
    catch(TokenRetrievalFailedException e) // The request body must contain the following parameter: 'client_assertion' or 'client_secret'.
    { }
    return BadRequest();
}