4

我正在尝试实现一个自定义角色提供程序,我找到了一个教程并遵循了它。这是链接: http ://techbrij.com/custom-roleprovider-authorization-asp-net-mvc

当我尝试使用不存在的用户帐户登录时,不会出现错误消息。这是我当前的代码。

这是登录的代码:

[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                using (SampleDBEntities objContext = new SampleDBEntities())
                {
                    var objUser = objContext.Users.FirstOrDefault(x => x.AppUserName == model.UserName && x.Password == model.Password);
                    if (objUser == null)
                    {
                        ModelState.AddModelError("LogOnError", "The user name or password provided is incorrect.");
                    }
                    else
                    {
                        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                           && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            ModelState.AddModelError("LogOnError", "The user name or password provided is incorrect.");
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            //Redirect to default page

                            //return RedirectToAction("RedirectToDefault");
                            return RedirectToAction("Index", "Home");
                        }
                    }
                }
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }

以下是提供程序的实现代码:

public class MyRoleProvider : RoleProvider
    {
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }


        public override string[] GetRolesForUser(string username)
        {
            using (SampleDBEntities objContext = new SampleDBEntities())
            {
                var objUser = objContext.Users.FirstOrDefault(x => x.AppUserName == username);
                if (objUser == null)
                {
                    return null;
                }
                else
                {
                    string[] ret = objUser.Roles.Select(x => x.RoleName).ToArray();
                    return ret;
                }
            }
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}

先生/女士,您的回答会很有帮助。谢谢++

4

1 回答 1

7

ModelState您已在键下添加了错误"LogOnError"

ModelState.AddModelError("LogOnError", "The user name or password provided is incorrect.");

Html.ValidationMessage仅当您的视图中有相应的帮助器时才会出现此错误:

@Html.ValidationMessage("LogOnError")

如果您希望错误出现在Html.ValidationSummary()帮助程序中,请使用空白键:

ModelState.AddModelError("", "The user name or password provided is incorrect.");
于 2013-01-27T17:25:45.050 回答