我正在尝试实现一个自定义角色提供程序,我找到了一个教程并遵循了它。这是链接: 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();
}
}
}
先生/女士,您的回答会很有帮助。谢谢++