问题描述
我正在调用更新会话的控制器方法。在调用 Microsoft Graph API 方法获取登录用户之前,我可以使用 HttpContext.Session.SetString("Percentage","10");
正确设置会话。但是当我想在像 LoggedInUser = await _graphServiceClient.Me.Request().GetAsync();
这样的 Microsoft Graph API 调用之后更新会话时,我想使用 HttpContext.Session.SetString("Percentage","40");
再次更新会话。现在我遇到了这样的异常:
{"IFeatureCollection 已被释放。\r\n对象名称:'Collection'。"}
我也知道 HttpContext 仅适用于单个 http 请求上下文。我只是想知道这个替代方案来实现针对多个 http 请求上下文的会话。
任何帮助都会更有价值。请帮助我为此花了很多时间,但找不到任何东西。
这是我的控制器操作方法:
private async Task<IActionResult> CallAPI_Click()
{
try
{
//Setting Session Here
HttpContext.Session.SetString("Percentage","10");
//Calling Microsoft Graph API to get Logged in User
LoggedInUser = await _graphServiceClient.Me.Request().GetAsync();
//Updating Session Here and getting exception here
HttpContext.Session.SetString("Percentage","40");
}
catch (Exception ex)
{
//{"IFeatureCollection has been disposed.\r\nObject name: 'Collection'."}
Console.WriteLine(ex.Message);
}
}
这是我的 Startup.cs 文件:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
string[] initialScopes = Configuration.GetValue<string>("DownstreamApi:Scopes")?.Split(' ');
services
// Use OpenId authentication
.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
// Specify this is a web app and needs auth code flow
.AddMicrosoftIdentityWebApp(Configuration)
// Add ability to call web API (Graph)
// and get access tokens
.EnabletokenAcquisitionToCallDownstreamApi(options =>
{
Configuration.Bind("AzureAd",options);
},initialScopes)
// Use in-memory token cache
// See https://github.com/AzureAD/microsoft-identity-web/wiki/token-cache-serialization
.AddInMemoryTokenCaches()
.AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"));
// Require authentication
services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new Authorizefilter(policy));
})
// Add the Microsoft Identity UI pages for signin/out
.AddMicrosoftIdentityUI();
services.AddScoped<AppData>();
services.AddRazorPages();
var path = Directory.GetCurrentDirectory();
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo($"{path}\\DataProtectionKeys\\Keys.xml"));
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,IWebHostEnvironment env,ILoggerFactory loggerFactory)
{
var path = Directory.GetCurrentDirectory();
loggerFactory.AddFile($"{path}\\SentimentLogs\\Sentiment_Log.txt");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios,see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)