Ran*_*rez 4 asp.net roleprovider asp.net-roles asp.net-mvc-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);
}
Run Code Online (Sandbox Code Playgroud)
以下是提供程序实现的代码:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
先生/女士,你的答案会有很大的帮助.谢谢++
您已ModelState在密钥下添加了错误"LogOnError":
ModelState.AddModelError("LogOnError", "The user name or password provided is incorrect.");
Run Code Online (Sandbox Code Playgroud)
只有Html.ValidationMessage在视图中有相应的帮助程序时,才会出现此错误:
@Html.ValidationMessage("LogOnError")
Run Code Online (Sandbox Code Playgroud)
如果您希望错误出现在Html.ValidationSummary()帮助程序中,请使用空白键:
ModelState.AddModelError("", "The user name or password provided is incorrect.");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4414 次 |
| 最近记录: |