question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] How to get OBO token from an event handler

See original GitHub issue

How can I get an OBO access token in an event handler like:

[HttpPost]
public IActionResult ProcessData(string data)
{
    var processor = new Processor();

    processor.Finished += async (s, e) =>
    {
        var accessToken = await this
            ._tokenAcquisition
            .GetAccessTokenForUserAsync(new[] { "Files.ReadWrite" }, user: this.User);

        await WriteToOneDrive(accessToken, e.Data);
    };

    processor.processAsync(data); // Run async without awaiting

    return NoContent();
}

In this case the this.User is already disposed, and even without it, GetAccessTokenForUserAsync crashes with NullObjectException.

My process takes a very long time (hours).

How can I get the token inside a callback when I need it?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:23 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jmprieurcommented, Aug 13, 2020

I think that the problem is that the HttpContext is null, or HttpContext.Items is null:

https://github.com/AzureAD/microsoft-identity-web/blob/41195539af65836b464b063c186842896ab15e0e/src/Microsoft.Identity.Web/HttpContextExtensions.cs#L30

We should probably guard against these cases:

 return httpContext?.Items?[Constants.JwtSecurityTokenUsedToCallWebApi] as JwtSecurityToken;

as if the token is returned null, the AcquireTokenSilent will be tried, which should work for this scenario.

@jennyf19 @pmaytak @henrik-me I’m tempted to take this in 0.3.0-preview if you don’t mind, as this would unblock our partner.

1reaction
hrazmsftcommented, Sep 3, 2020

Hi @jennyf19 I still can’t get a token inside a callback with the latest master (error below).

I’ve open a repo with sample code with repro of the problem here

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

get token in event handler · AzureAD/microsoft-identity-web ...
How to get an OBO token in an event handler or a long running process. Principle ... When the call is done, and...
Read more >
Microsoft identity platform and OAuth 2.0 On-Behalf-Of flow
This article describes how to use HTTP messages to implement service to service authentication using the OAuth2.0 On-Behalf-Of flow.
Read more >
MSAL.NET OBO refresh token problems - oauth 2.0
When it's called with the jwtBearerToken, it will successfully call AcquireTokenOnBehalfOf() and the token is cached and a result returned, but ...
Read more >
MSAL Python 1.23.0 documentation
Handle a token obtaining event, and add tokens into cache. You can subclass it to add new behavior, such as, token serialization. See...
Read more >
On-behalf-of flows with MSAL.NET
The OBO call is done by calling the AcquireTokenOnBehalfOf(IEnumerable<String>, UserAssertion) method on the IConfidentialClientApplication ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found