.NET Core 2.0 AddToRoleAsync - 用户名无效,只能包含字母或数字

时间:2018-02-09 18:39:08

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

我正在使用.NET Core 2.0 MVC和Entity Framework与个人用户帐户。默认情况下,用户名与电子邮件地址相同。我在Startup.cs中使用以下内容来创建角色

private async Task CreateRoles(IServiceProvider serviceProvider)
{
    //adding custom roles
    var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
    var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
    string[] roleNames = { "Admin", "User" };
    IdentityResult roleResult;
    foreach (var roleName in roleNames)
    {
        //creating the roles and seeding them to the database
        var roleExist = await RoleManager.RoleExistsAsync(roleName);
        if (!roleExist)
        {
            roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
        }
    }

    //creating a super user who could maintain the web app
    var poweruser = new ApplicationUser
    {
        UserName = Configuration.GetSection("UserSettings")["UserEmail"],
        Email = Configuration.GetSection("UserSettings")["UserEmail"]
    };

    string UserPassword = Configuration.GetSection("UserSettings")["UserPassword"];
    var _user = await UserManager.FindByEmailAsync(Configuration.GetSection("UserSettings")["UserEmail"]);

    if (_user == null)
    {
        var createPowerUser = await UserManager.CreateAsync(poweruser, UserPassword);
        if (createPowerUser.Succeeded)
        {
            //here we tie the new user to the "Admin" role 
            await UserManager.AddToRoleAsync(poweruser, "Admin");

        }
    }
}

并从Startup.cs中的Configure方法调用它。添加的角色很好,角色已添加到管理员。

但是,当我尝试使用ApplicationUsersController中的方法await _userManager.AddToRoleAsync(applicationUser, "Admin");以编程方式向用户添加角色时,我收到错误

  

用户名无效,只能包含字母或数字

这里的.NET Core 2.0文档https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-configuration?tabs=aspnetcore2x表示“@”和/或“。”默认情况下,可能导致错误的字符包含在AllowedUserNameCharacters中作为有效字符。我无法尝试其他事情。

这是我的ApplicationUsersController代码:

[Authorize(Roles = "Admin")]
public class ApplicationUsersController : Controller
{
    private readonly ApplicationDbContext _context;
    private readonly UserManager<ApplicationUser> _userManager;

    public ApplicationUsersController(UserManager<ApplicationUser> userManager, ApplicationDbContext context)
    {

        _userManager = userManager;
        _context = context;
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(string id, [Bind("Id,Name,Email,IsAdmin,ConcurrencyStamp,SecurityStamp")] ApplicationUser applicationUser)
    {
        if (id != applicationUser.Id)
        {
            return NotFound();
        }

        if (ModelState.IsValid)
        {
            try
            {
                applicationUser.DateUpdated = DateTime.Now;
                applicationUser.NormalizedEmail = applicationUser.Email.ToUpper();
                _context.Update(applicationUser);
                await _context.SaveChangesAsync();

                if (applicationUser.IsAdmin)
                {
                    var x = await _userManager.AddToRoleAsync(applicationUser, "Admin");
                    if (!x.Succeeded)
                    {
                        string s = "";
                    }
                }
                else
                {
                    await _userManager.AddToRoleAsync(applicationUser, "User");
                }

            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ApplicationUserExists(applicationUser.Id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction(nameof(Index));
        }
        return View(applicationUser);
    }

0 个答案:

没有答案