[ASP.NET 2.2] [C#]将用户角色添加到ApplicationUser表

时间:2019-02-09 15:19:56

标签: c# asp.net asp.net-mvc asp.net-identity

我在编程方面很新鲜,因此对愚蠢的问题感到抱歉,但是我对这段代码有疑问。编译器没有给出任何错误。但是,当我进入页面时,我遇到了HTTP错误405。为什么这样做以及如何解决?这是我的代码:

 [HttpPost]
    public async Task<IActionResult> Index()
    {
        var users = await _userManager.Users.ToListAsync();

        foreach (var v in users)
        {
            var roles = await _userManager.GetRolesAsync(v);
            var userId = await _userManager.GetUserIdAsync(v);

            var role = "";

            foreach (var r in roles)
            {
                if (!role.Contains(",")) { role = r; }
                else { role = "," + r; }
            }

            var user = _context.Users.Where(u => u.Id == userId).FirstOrDefault();
            user.UserRoles = role;
            _context.SaveChanges();
        }
        var usersList = _context.Users.ToList();
        return await Task.Run(() => View());
    }

或者也许有更好的方法将UserRole添加到dbo.AspNetUsers?

0 个答案:

没有答案