问题
I have the following code:
using IdentityModel.Client;
using Microsoft.AspNet.Identity;
var tokenClient = new HttpClient();
var testUserName = System.Web.HttpContext.Current.User.Identity.GetUserName(); // This returns a value
var tokenResult = await tokenClient.RequestAuthorizationCodeTokenAsync(
new AuthorizationCodeTokenRequest
{
Address = address,
Code = authorizationCode,
ClientId = clientId,
ClientSecret = clientSecret,
GrantType = "authorization_code",
RedirectUri = redirectUri
});
current = System.Web.HttpContext.Current; // This fails, Current is null
testUserName = System.Web.HttpContext.Current.User.Identity.GetUserName(); // Fails as null
So it seems the HttpContext object is being reset due to this API call. Any thoughts how to fix this?
Thanks in advance.
EDIT / More Info.
This happens with "PostAsync" as well.
var postContent = new FormUrlEncodedContent(post);
var userName = HttpContext.Current.User.Identity.GetUserName(); // returns value
var response = await client.PostAsync(IdentityUrl() + TokenEndPoint,postContent);
userName = HttpContext.Current.User.Identity.GetUserName(); // returns null
回答1:
Answer is here:
tokenClient.RequestAuthorizationCodeTokenAsync is clearing out HttpContext, can this be prevented?
I added:
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
Happy days. Keep well all.
来源:https://stackoverflow.com/questions/64371054/tokenclient-requestauthorizationcodetokenasync-is-clearing-out-httpcontext-can