本文介绍了tokenClient.RequestAuthorizationCodeTokenAsync正在清除HttpContext,可以防止这种情况发生吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
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
因此,由于此API调用,似乎正在重置HttpContext对象.有什么想法要解决吗?
So it seems the HttpContext object is being reset due to this API call. Any thoughts how to fix this?
提前谢谢.
编辑/更多信息.
这是通过"PostAsync"发生的;也是
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
推荐答案
答案在这里:
tokenClient.RequestAuthorizationCodeTokenAsync正在清除HttpContext,可以防止这种情况吗?a>
我添加了:
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
快乐的日子.保持一切健康.
Happy days. Keep well all.
这篇关于tokenClient.RequestAuthorizationCodeTokenAsync正在清除HttpContext,可以防止这种情况发生吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!