tokenClient.RequestAuthorizationCodeTokenAsync is clearing out HttpContext, can this be prevented?

笑着哭i 提交于 2021-02-11 12:22:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!