这是我想用 ASP.Net MVC 3 实现的目标:
在用户可以积极使用我的网站之前,我希望他们填写表格并提供一些详细信息。除非他们这样做,否则他们会被重定向到表单网站。
我认为通过注册将 IsApproved 标志设置为 false 的帐户是可行的,但实际上这些用户根本无法登录(Membership.ValidateUser(model.UserName, model.Password)
总是返回 false)。
表单身份验证中是否有任何机制支持这种情况?还是我必须在自定义数据库表中跟踪它?
提前致谢!
编辑:感谢 MystereMan(先阅读他的回复)和其他一些关于 SO 的精彩帖子,这里是 ActionFilter(属性及其用法):
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class RedirectUserInRoleAttribute : ActionFilterAttribute
{
public string RoleName { get; set; }
public string TargetControllerName { get; set; }
public string TargetActionName { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
MembershipUser user = Membership.GetUser();
if (user != null)
{
string[] roles = Roles.GetRolesForUser(user.UserName);
if (roles.Any(x => x == RoleName))
{
RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("action", TargetActionName);
redirectTargetDictionary.Add("controller", TargetControllerName);
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
}
}
}
[Authorize]
[RedirectUserInRole(RoleName = "Pending", TargetControllerName = "Home", TargetActionName = "Index")]
public ActionResult ChangePassword()
{
//...
}
[Authorize]
[RedirectUserInRole(RoleName = "Member", TargetControllerName = "Home", TargetActionName = "Index")]
public ActionResult Confirm()
{
// show confirm form here!
}