gpt4 book ai didi

c# - 等待调用后异步的 HttpContext.Current null

转载 作者:太空宇宙 更新时间:2023-11-03 19:03:08 24 4
gpt4 key购买 nike

HttpContext.Current 在等待调用后异步为 null。

这是我的代码:

if (!string.IsNullOrEmpty(securityGroupName))
{
// To remove the domain name from the security group name.
string securityGroupDisplayName = securityGroupName.Split('\\')[1];
string serviceSecurityGroupId = await this.graphApiClient.GetGroupIdAsync(securityGroupDisplayName).ConfigureAwait(false);

if (!string.IsNullOrEmpty(serviceSecurityGroupId))
{
Task securityGroupRoleAddTask = this.CheckMembershipAndAddRole(serviceSecurityGroupId, userId, securityGroupName);
Task flightAdminRoleAddTask = this.CheckMembershipAndAddRole(FlightAdminSecurityGroupId, userId, FlightAdminRoleName);
Task.WaitAll(securityGroupRoleAddTask, flightAdminRoleAddTask);
}
else
{
LoggingUtilities.Logger.TraceInformation("Azure AD id does not exist for the security group: {0}.", securityGroupName);
await this.CheckMembershipAndAddRole(FlightAdminSecurityGroupId, userId, FlightAdminRoleName).ConfigureAwait(false);
}
}
else
{
LoggingUtilities.Logger.TraceInformation("Security group name is not valid, checking for flight admin role for the user: {0}.", userAlias);
await this.CheckMembershipAndAddRole(FlightAdminSecurityGroupId, userId, FlightAdminRoleName).ConfigureAwait(false);
}

// Add the flight privileged users role to be able to verify the user is authorized for the role.
string flightPrivilegedUsersRoleName = RoleRepository.Instance.GetByName(Constants.FlightPrivilegedUsersRoleKey).Name;
if (!this.roles.Contains(flightPrivilegedUsersRoleName, StringComparer.OrdinalIgnoreCase))
{
LoggingUtilities.Logger.TraceInformation("Adding flight privileged users to roles list for the user: {0}.", userAlias);
this.roles.Add(flightPrivilegedUsersRoleName);
}

if (userAlias != null)
{
LoggingUtilities.Logger.TraceInformation("Check security group memberships and assign roles for the user: {0}.", userAlias);
var newPrincipal = new GenericPrincipal(new GenericIdentity(userAlias), this.roles.ToArray());
Thread.CurrentPrincipal = newPrincipal;
HttpContext.Current.User = newPrincipal;
}

关于 web.config 条目中以下条目的建议没有帮助:

<system.web>
<compilation debug="false" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" enableVersionHeader="false" requestPathInvalidCharacters="&lt;,&gt;,%,&amp;,\,?" />
</system.web>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>

如有任何关于如何解决此问题的建议,我们将不胜感激。

最佳答案

删除您的 .ConfigureAwait(false) 调用。他们是给你带来麻烦的原因。当您调用 .ConfigureAwait(false) 时,它会告诉 C# 您不关心使用哪个线程来完成异步调用。您最终进入另一个没有 HttpContext.Current 上下文的线程,因为它是一个线程池线程,而不是 ASP.NET 线程。

关于c# - 等待调用后异步的 HttpContext.Current null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32731197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com