交易未结束的地方?

时间:2017-06-24 19:57:52

标签: c# asp.net-identity claims-based-identity

我想根据自己的角色为每个用户添加声明,并提供以下代码:

    public async Task SetClaims()
    {
        var users = UserManager.Users;
        foreach (var user in users)
        {
            var roleList = await UserManager.GetRolesAsync(user.Id);
            foreach (var role in roleList)
            {
                //await Utils.AddUserToRoleAsync(user.Id, role, UserManager);
                var userId = user.Id;
                var claims = await UserManager.GetClaimsAsync(userId);
                foreach (var claim in claims)
                {
                    await UserManager.RemoveClaimAsync(userId, claim);
                }
                switch (role)
                {
                    case "Superadmin":
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.create"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.delete"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.edit"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.viewList"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.viewDetail"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.create.selectCompany"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.create.selectRole.fullList"));
                        break;

                    case "CompanyAdmin":
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.create"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.delete"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.edit"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.viewList"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.viewDetail"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.create.selectRole.rolesInsideCompany"));
                        break;

                    case "CompanyManager":
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.create"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.delete"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.edit"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.viewList"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.viewDetail"));
                        await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.create.selectRole.rolesInsideCompany"));
                        break;

                    default:
                        break;
                }
            }
        }
    }

但我在网上收到错误:

await UserManager.AddClaimAsync(userId, new System.Security.Claims.Claim("Permission", "users.create"));
  

不允许使用新事务,因为还有其他线程正在运行   在会议中。

     

描述:执行期间发生了未处理的异常   当前的网络请求。请查看堆栈跟踪了解更多信息   有关错误的信息以及它在代码中的起源。

     

异常详细信息:System.Data.SqlClient.SqlException:新事务   是不允许的,因为会话中还有其他线程在运行。

据我所知,交易未结束。在哪里以及如何正确地做到这一点?

0 个答案:

没有答案