Accoun / Register Value不能为空错误

时间:2017-09-15 08:57:31

标签: c# asp.net-mvc

注册新帐户时出现此问题。我在那里创建了一个下拉列表来选择一个角色(管理员,用户等)。

当我创建一个新帐户时,如果我写一个字母作为密码,则会显示此信息(正常工作): Here it is

当我创建没有数字的密码时,大写字母和非字母符号。它默认情况下也应该声明类似的错误。说的话:“你的密码没有大写字母,等等......” 但它的作用是发送此错误。 Here

如果我正确地写了密码,如:“password1A_”那么它可以正常工作,创建一个没有任何问题的帐户。

我该如何解决此错误? 我没有插入任何东西,因为我没有问题所在,我不会在这里插入所有注册代码,因为默认情况下它太多了。

这是我的注册方法:

    [AllowAnonymous]
    public ActionResult Register()
    {
        List<SelectListItem> list = new List<SelectListItem>();
        foreach (var role in RoleManager.Roles)
            list.Add(new SelectListItem() { Value = role.Name, Text = role.Name });
        ViewBag.Roles = list;

        return View();
    }

    //
    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                result = await UserManager.AddToRoleAsync(user.Id, model.RoleName);
                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

像这样更新您的帖子操作方法

//
        // POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    result = await UserManager.AddToRoleAsync(user.Id, model.RoleName);
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }
            // Populate ViewBag.Roles that needed by view
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var role in RoleManager.Roles)
                list.Add(new SelectListItem() { Value = role.Name, Text = role.Name });
            ViewBag.Roles = list;
            // If we got this far, something failed, redisplay form
            return View(model);
        }

希望这个帮助