Question

I'm trying to implement a custom role provider and I found a tutorial and followed it. Here is the link: http://techbrij.com/custom-roleprovider-authorization-asp-net-mvc

When I try to login using a user account that does not exist, the error message does not appear. Here is my current code.

Here is code for the login:

[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);
        }

Here is the code for the implementation of the provider:

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();
        }
    }
}

Sir/Ma'am your answers would be of great help. Thank you++

Was it helpful?

Solution

You have added the error in ModelState under the key "LogOnError":

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

This error will appear only if you have a corresponding Html.ValidationMessage helper in your view:

@Html.ValidationMessage("LogOnError")

If you want the error to appear in the Html.ValidationSummary() helper use a blank key:

ModelState.AddModelError("", "The user name or password provided is incorrect.");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top