ITW*_*ker 6 c# asp.net asp.net-mvc asp.net-identity
我有一个名为的类PasswordChangeChecker.cs,它有一个从数据库返回的方法,无论用户是否更改了密码.该方法的签名是:
public bool IsPasswordChangedFromInitial(string IdOfUser)
IdOfUserIdentity框架用户的Id字段在哪里.如果返回true,则表示不应显示更改密码页面,否则应导航到更改密码表单.一旦用户成功更改了密码,数据库标志就会被正确设置,并且不会再次提示他们更改密码(除非管理员手动强制更改).如何将此方法放在RouteConfig.cs文件中,目前我所拥有的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace IdentityDevelopment
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何在defaults参数中添加条件构造,以便我可以使用该IsPasswordChangedFromInitial方法来决定是否转到密码更改页面?该页面位于/Account/ChangePassword.
编辑
根据评论,针对我的特定需求的相应操作方法是(我省略了不相关的代码):
登录发布动作:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginModel details, string returnUrl)
{
if (ModelState.IsValid)
{
AppUser user = await UserManager.FindAsync(details.Name,
details.Password);
if (user == null)
{
AppUser userByName = await UserManager.FindByNameAsync(details.Name);
if(userByName == null)
{
ModelState.AddModelError("", "Invalid username.");
}
else
{
//If this else is reached, it means the password entered is invalid.
//code for incrementing count of failed attempts and navigation to lock out page if needed
}
}
else
{
if(user.LockedOut)
{
//navigate to locked out page
}
else
{
PasswordChangeChecker PassCheck = new PasswordChangeChecker();
string userId = user.Id.ToString();
bool proceed = PassCheck.IsPasswordChangedFromInitial(userId);
//Authorize the user
ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user,
DefaultAuthenticationTypes.ApplicationCookie);
ident.AddClaims(LocationClaimsProvider.GetClaims(ident));
ident.AddClaims(ClaimsRoles.CreateRolesFromClaims(ident));
AuthManager.SignOut();
AuthManager.SignIn(new AuthenticationProperties
{
IsPersistent = false
}, ident);
//persist login into db code
if (proceed)
{
//reset failed logins count
return Redirect(returnUrl);
}
else
{
return ChangePassword();
}
}
}
}
ViewBag.returnUrl = returnUrl;
return View(details);
}
Run Code Online (Sandbox Code Playgroud)
ChangePassword()得到行动:
[HttpGet]
[Authorize]
public ActionResult ChangePassword()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
不知何故,返回的视图是RouteConfig.cs代替ChangePassword.cshtml页面的视图.谢谢.
我会用全局动作过滤器来做到这一点
你可以用方法创建一个动作过滤器
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (showChagePwPage)
{
//redirect to the change password page
filterContext.Result = new RedirectToActionResult("ChangePassword", "Account");
}
base.OnActionExecuting(filterContext);
}
Run Code Online (Sandbox Code Playgroud)
然后将其添加到全局操作过滤器中
GlobalFilters.Filters.Add(yourFilterContext);
Run Code Online (Sandbox Code Playgroud)