gpt4 book ai didi

c# - ASP.NET MVC : How to default to a page given certain condition?

转载 作者:太空狗 更新时间:2023-10-29 21:48:58 26 4
gpt4 key购买 nike

我有一个名为 PasswordChangeChecker.cs 的类,它有一个从数据库返回的方法,无论用户是否更改了密码。该方法的签名是:

public bool IsPasswordChangedFromInitial(string IdOfUser)

其中 IdOfUser 是来自身份框架用户的 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 }
);
}
}
}

如何将条件构造添加到 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);
}

ChangePassword() 获取操作:

[HttpGet]
[Authorize]
public ActionResult ChangePassword()
{
return View();
}

不知何故,返回的 View 是 RouteConfig.cs 中的 View ,而不是 ChangePassword.cshtml 页面中的 View 。谢谢。

最佳答案

我会用全局 Action 过滤器来做

你可以制作一个action filter用方法

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (showChagePwPage)
{
//redirect to the change password page
filterContext.Result = new RedirectToActionResult("ChangePassword", "Account");
}

base.OnActionExecuting(filterContext);
}

然后通过

将其添加到全局 Action 过滤器中
GlobalFilters.Filters.Add(yourFilterContext);

关于c# - ASP.NET MVC : How to default to a page given certain condition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40025093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com