Microsoft Identity Framework在Postgres数据库中将角色分配给用户时出错-Cancell



嗨,我在使用Identity Framework 5.0.5、.net5.0.5和Postgres SQL数据库为用户分配角色时遇到问题。

用于将角色分配给用户的代码可以在下面看到,下面有错误

如果可能的话,请帮忙。

非常感谢!

{
if (!await roleManager.RoleExistsAsync("Admin"))
{
await roleManager.CreateAsync(new IdentityRole
{
Name = "Admin"
});
}

ApplicationUser user = await userManager.FindByIdAsync(userId);
if (isAdmin)
{
await userManager.AddToRoleAsync(user, "Admin");
}
else
{
await userManager.RemoveFromRoleAsync(user, "Admin");
}
}
}

services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
Configuration.GetConnectionString("DefaultConnection"), 
builder => builder.EnableRetryOnFailure(50, TimeSpan.FromSeconds(5), null)));

System.ObjectDisposedException: The CancellationTokenSource has been disposed.
at System.Threading.CancellationTokenSource.ThrowObjectDisposedException()
at System.Threading.CancellationTokenSource.CancelAfter(Int32 millisecondsDelay)
at System.Threading.CancellationTokenSource.CancelAfter(TimeSpan delay)
at Npgsql.Util.ResettableCancellationTokenSource.Stop()
at Npgsql.NpgsqlReadBuffer.<Ensure>g__EnsureLong|40_0(NpgsqlReadBuffer buffer, Int32 count, Boolean async, Boolean readingNotifications)
at Npgsql.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|194_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at SearchDataScreening.Pages.UserManagement.OnGetAdminControl(String userId, Boolean isAdmin) in /Users/dewaldthattingh/GitHubRepos/TPS/SearchScreening/SearchDataScreening/Pages/UserManagement.cshtml.cs:line 50
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__140_1(Object state)
at System.Threading.QueueUserWorkItemCallback.<>c.<.cctor>b__6_0(QueueUserWorkItemCallback quwi)
at System.Threading.ExecutionContext.RunForThreadPoolUnsafe[TState](ExecutionContext executionContext, Action`1 callback, TState& state)
at System.Threading.QueueUserWorkItemCallback.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

请尝试将其更改为不使用下面的等等待方法

bool roleExist =  roleManager.RoleExistsAsync("Admin").Result;
if (!roleExist)
{
var x =  roleManager.CreateAsync(new IdentityRole
{
Name = "Admin"
}).Result;
}
if (isAdmin)
{
var user =  userManager.FindByIdAsync(userId).Result;
if (user != null)
{
var x = userManager.AddToRoleAsync(user, "Admin").Result;
}
}
else
{
var user =  userManager.FindByIdAsync(userId).Result;
if (user != null)
{
var x = userManager.RemoveFromRoleAsync(user, "Admin").Result;
}
}

相关内容

  • 没有找到相关文章

最新更新