I am trying to implement following logic:
Whenever a new user is created, a default hardcoded password would be assigned. If the new created user logged in, the system would FORCE the user to change their default password
I have successfully achieved the logic above with this code in my controllers:
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
//Check if authenticated
if (filterContext.HttpContext.User.Identity.IsAuthenticated && !filterContext.HttpContext.Request.RawUrl.Equals("/Account/ChangePassword") && !filterContext.HttpContext.Request.RawUrl.Equals("/Account/LogOff"))
{
//Get user Data
MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
if (currentUser.GetPassword().Equals("password"))
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "Account" },
{ "action", "ChangePassword" }
});
}
}
}
However, now i have to copy paste this code to all of my controllers. Does this disrespect the DRY principle in mvc3 ?
If Yes, Where or how should i place this code so it can be globally implemented to all of my controllers? I tried to paste it in Global.asax but i got an error that said "No Suitable Method Found to Override"