在aspnet身份登录失败时,用户名仍然存在

时间:2015-04-15 05:56:20

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

我有登录方法,当用户输入错误的详细信息时,它会像以前一样提供空的登录页面。

这里是Login方法

    //
    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {


        if (ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.UserName, model.Password);

            var identity = (ClaimsIdentity)User.Identity;
            IEnumerable<Claim> claims = identity.Claims;

            if (user != null)
            {
                if (user.Status == true && user.ConfirmedEmail == true)
                {

                    await SignInAsync(user, model.RememberMe);

                    if (String.IsNullOrEmpty(returnUrl))
                    {
                        if (UserManager.IsInRole(user.Id, "HEC_Admin"))
                        {
                            return RedirectToAction("U_Index", "HEC");
                        }
                        //role Admin go to Admin page
                        if (UserManager.IsInRole(user.Id, "HEI_User"))
                        {
                            return RedirectToAction("StudentIndex", "HEI");
                        }



                    }

                    else
                    {
                        return RedirectToLocal(returnUrl);
                    }


                }
                else if (user.ConfirmedEmail == true && user.Status == false)
                {
                    ModelState.AddModelError("", "The account is currently Inactive. Please contact the Administrator");
                    return View(model);
                }

                else if (user.ConfirmedEmail == false)
                {
                    ModelState.AddModelError("", "Please click on the link provided in the confirmation email, to activate your account");
                    return View(model);
                }

                else {
                    ModelState.AddModelError("", "The account is currently Inactive. Please contact the Administrator");
                    return View(model);
                }
            }
            else
            {
                ModelState.AddModelError("", "The Username or Password is invalid.");
                return View(model);
            }
        }

        else if ((string.IsNullOrEmpty(model.UserName)) && (string.IsNullOrEmpty(model.Password)))
        {
            ModelState.AddModelError("", "Please enter the Username and Password.");
            return View(model);
        }

        else if (string.IsNullOrEmpty(model.UserName))
        {
            ModelState.AddModelError("", "Please enter the Username.");

        }

        else if (string.IsNullOrEmpty(model.Password))
        {
            ModelState.AddModelError("", "Please enter the Password.");
            return View(model);
        }


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

这里是视图

    <div class="form-box" id="login-box">



    @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
    @Html.AntiForgeryToken()



  <div class="body bg-gray">
       @Html.ValidationSummary(true)
    <div class="form-group"> 
        <input type="text" value="" name="UserName"  id="validateHecUser_username" class="form-control" placeholder="Username" >

    </div>
    <div class="form-group"> 
        <input type="password" value="" name="password" id="validateHecUser_password" class="form-control" placeholder="Password" >   

    </div>


  </div>


    }
</div>

我想在用户登录失败时保留用户名,如何在aspnet身份会员提供商中保留用户名

1 个答案:

答案 0 :(得分:1)

按模型属性分配输入值,例如

<div class="form-group"> 
  <input type="text" value="@Model.UserName" name="UserName"  id="validateHecUser_username" class="form-control" placeholder="Username" >
</div>

<div class="form-group"> 
  <input type="password" value="@Model.Password" name="password" id="validateHecUser_password" class="form-control" placeholder="Password" >
</div>

但最好使用Html Helpers渲染输入控件,例如:

<div class="form-group">
  @Html.TextboxFor(model => model.UserName)
</div>
<div class="form-group">
  @Html.TextboxFor(model => model.Password)
</div>

注意:您的视图必须是强类型。

希望它有所帮助,谢谢。

相关问题