Repository navigation
Description
Which version of Microsoft Identity Web are you using?
Note that to get help, you need to run the latest version.
Where is the issue?
- Web app
- Sign-in users
- Sign-in users and call web APIs
- Web API
- Protected web APIs (validating tokens)
- Protected web APIs (validating scopes)
- Protected web APIs call downstream web APIs
- Token cache serialization
- In-memory caches
- Session caches
- Distributed caches
- Other (please describe)
Is this a new or an existing app?
This is a new asp.net core service web app.
Repro
- Use
RequiredScopesattribute
[Authorize]
[Route("auth")]
public class AuthController : Controller
{
/// ...
[RequiredScope("access_as_user")]
[HttpPost("token")]
public async Task<IActionResult> PostToken([FromBody] PostTokenRequestBody body)
{
}
}Usage of RequiredScope attribute will throw authentication exception and fall into our exception filter, causing Http request exception saying "error while copying content to a steam."
public class SimpleAuthExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
// Called after an action has thrown an System.Exception.
// construct result statusCode / content / detailed message , etc.
}
}Error Message:
Message:
System.Net.Http.HttpRequestException : Error while copying content to a stream.
----> System.IO.IOException : No message provided
----> System.UnauthorizedAccessException : IDW10203: The 'scope' or 'scp' claim does not contain scopes 'access_as_user' or was not found.
Stack Trace:
HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
AuthControllerTests.PostToAuthTokenApi[T](HttpClient client, StringContent body) line 97
- Use
VerifyUserHasAnyAcceptedScopeextension method
[Authorize]
[Route("auth")]
public class AuthController : Controller
{
/// ...
[HttpPost("token")]
public async Task<IActionResult> PostToken([FromBody] PostTokenRequestBody body)
{
HttpContext.VerifyUserHasAnyAcceptedScope(access_as_user);
}
}Usage of RequiredScope attribute will also throw authentication exception and fall into our exception filter, causing Http request exception saying "error while copying content to a steam." But the error message is a bit different saying "status code cannot be set".
Error Message:
Message:
System.Net.Http.HttpRequestException : Error while copying content to a stream.
----> System.IO.IOException : No message provided
----> System.InvalidOperationException : The status code cannot be set, the response has already started.
Stack Trace:
HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
AuthControllerTests.PostToAuthTokenApi[T](HttpClient client, StringContent body) line 97
Expected behavior
Verification of scope should not throw error. It should return status code directly so it won't fall into developer's customized Exception handler.
Actual behavior
Throw error and the lock of context seems to cause the "error while copying content to a steam" problem.
Activity
jennyf19 commentedon Jun 11, 2021
Fixed in 1.13 release.