- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我做了more than one
API调用同时出现错误
A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method in this context. Any instance members are not guaranteed to be thread-safe.
从var identity = await base.currentUserService.CreateIdentity(user);
抛出异常因为两个请求是同时发出的,并且前一个请求的 CreateIdentity 方法还没有完成!
如何解决上述场景?
我想等IsValidRequest
调用CustomKeyAuthenticationAttribute
在上一个请求完成之前。
这里是示例代码
public class CustomKeyAuthenticationAttribute : AuthorizeAttribute
{
[Dependency]
public IAuthService AuthService { get; set; }
public override async Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
var isValid = await AuthService.IsValidRequest(key-from-request-header);
if(!isValid){
return;
}
await base.OnAuthorizationAsync(actionContext, cancellationToken);
}
}
// In side AuthService
public async Task<bool> IsValidRequest(string key)
{
if (key-in-db)
{
var user = await base.currentUserService.GetUserById(userId-from-request-headers);
var identity = await base.currentUserService.CreateIdentity(user);
var principal = new GenericPrincipal(identity.Result, new string[0]);
HttpContext.Current.User = principal;
return true;
}
return false
}
// Inside currentUserService.CreateIdentity method
public async Task<ClaimsIdentity> CreateIdentity(CustomUser user) {
return await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
}
编辑
stackTrace: " atSystem.Data.Entity.Internal.ThrowingMonitor.EnsureNotEntered() atSystem.Data.Entity.Internal.Linq.InternalSet
1.FindAsync(CancellationToken cancellationToken, Object[] keyValues) at System.Data.Entity.DbSet
1.FindAsync(CancellationTokencancellationToken, Object[] keyValues) atSystem.Data.Entity.DbSet1.FindAsync(Object[] keyValues) at Microsoft.AspNet.Identity.EntityFramework.EntityStore
1.GetByIdAsync(Objectid) atMicrosoft.AspNet.Identity.EntityFramework.UserStore6.<GetUserAggregateAsync>d__6c.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNet.Identity.TaskExtensions.CultureAwaiter
1.GetResult()atMicrosoft.AspNet.Identity.UserManager2.<GetRolesAsync>d__ac.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNet.Identity.TaskExtensions.CultureAwaiter
1.GetResult()atMicrosoft.AspNet.Identity.ClaimsIdentityFactory2.<CreateAsync>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()
最佳答案
我需要查看背后的代码
var identity = await base.currentUserService.CreateIdentity(user);
但我的猜测是您将 DbContext 设置为各个请求之间的共享实例。您需要为每个线程使用不同的上下文。
检查this question here for more information因为我认为这是同样的问题。
另一个类似的问题on entity framwork documentation
关于AuthorizeAttribute 上的 C# 线程安全 CreateIdentityAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59390740/
这是我的 CustomAuthorizeAttribute 类: public class CustomAuthorizeAttribute : AuthorizeAttribute {
TL;博士 :当 IdentityServer3 将表单发布到始终是根 url 的返回 url 时,MVC 应用程序如何知道在通过身份验证过程后重定向到某个操作? 此问题中的示例取自 Identity
在我们的 MVC 解决方案中,我们有两个 AuthorizeAttribute 的自定义实现 - 一个名为 BasicHttpAuthorizeAttribute 并已在生产中使用多年,另一个 Rol
我的应用程序中有一个自定义的 AuthorizeAttribute,它接受一个输入参数 bool UserIsOnline。此参数用于增加一个表字段,该字段包含有关上次用户交互时间的信息,即对于在后台
我有一个 ApiController 类,其中有 10 个公共(public)方法。 在这 10 个方法中,有 9 个需要 [Authorize(Roles="Admin")]。不需要的,不需要任何授
我目前正在尝试根据用户角色在新的 ASP MVC 5 应用程序中实现安全性。目标是防止用户在没有特定角色(或更高级别)的情况下访问某些 Controller 或 Controller 方法。根据到目前
我正忙着为我的名为 MyAuthorizeAttribute 的操作方法编写自己的自定义属性,我还在忙着编写代码,这是我的部分代码: [AttributeUsage(AttributeTargets.
我创建了一个自定义 AuthorizeAttribute 类来处理我的 MVC4 应用程序中的精细授权。 这是类: [AttributeUsage(AttributeTargets.Method, A
我已经从 AuthorizeAttribute 实现了 RoleAuthorize 类 public sealed class RoleAuthorize : AuthorizeAttribute {
我正在使用 MVC3/4。但这只是授权中的一般问题。 我有一个角色在数据库中名为“Trip Leader”,其中包含一个空格。 我尝试了 [Authorize(Roles="'Trip Leader'
我们想授权用户,如果他们被授权,我们想将他们的角色和权限添加到用户并将其添加到 IPrinciple 我们有两种方法可以做到这一点,一种是在 global.asax Application_Authe
我一直在互联网上寻找为什么我的自定义 AuthorizeAttribute 在我的 MVC WebApi 中不起作用。我看到有人在 SO 上问过这种事情,但还没有帮助我解决我的问题: [Attrib
我想使用标准的 AuthorizeAttribute(即不继承它)但使用自定义重定向。那可能吗?我应该在哪里检查 401 并重定向? 我试过添加 但它没有用。 最佳答案 在我的 A
在 Windows 域内网站点(使用 )上工作时,我遇到了以下问题: [Authorize(Roles = "Domain Users, Domain Admins")] public class
我有一个像这样的自定义 AuthorizeAttribute public class DevMode : AuthorizationFilterAttribute { public over
我试图阻止特定角色(比如 RoleA)中的特定用户访问特定操作。允许匿名用户访问,但不允许 RoleA 中的用户访问该操作。 所以我做了这样的事情: [AllowAnonymous] [CustomA
如果我做了more than one API调用同时出现错误 A second operation started on this context before a previous asynchro
我有一个自定义的 RequireHttpsAttribute 和一个自定义的 AuthorizeAttribute,我在 FilterConfig 中应用它们以确保所有 Controller 都使用
在我的 MVC 5 应用程序中,我按如下方式装饰我的 Controller : [Authorize] public class Controller { .. 但是,我的一个要求是使用 toke
我已经研究了一段时间,现在试图弄清楚如何在我的 View 中使用我的自定义 AuthorizeAttribute 类来显示和隐藏链接。我正在从 IsInRole 过渡到自定义 AuthorizeAtt
我是一名优秀的程序员,十分优秀!