gpt4 book ai didi

c# - MVC 5 Identity 2.0 在登录时显示配置文件完成

转载 作者:行者123 更新时间:2023-11-30 22:06:20 24 4
gpt4 key购买 nike

使用 MVC 5 和 Identity 2.0,我将自定义属性添加到 ApplicationUserClass,例如 FirstNameLastNameAddress .这些将是数据库中的新字段。当用户注册到应用程序时,他/她将仅输入电子邮件地址和密码。在他们注册并登录后,我想强制他们完成他们的个人资料,或者至少在他们每次登录时,他们应该被重定向到个人资料完成页面,在那里他们可以提及名字、姓氏和地址。在他们完成个人资料后,他们将不会在每次登录时被重定向到完整的个人资料页面。

类似于:

if UserProfile != Completed

go to CompleteProfilePage

else

go to MainPage

最佳答案

您可以尝试使用全局过滤器。这将不允许您的用户通过手动修改 URL 来绕过检查。

public class ProfileCompletionCheckAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//this will prevent redirect for still unauthenticated users
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
return;

//replace these to actual values of your profile completion action and controller
string actionOfProfilePage = "Index";
string controlerOfProfilePage = "Home";

bool areWeAlreadyInProfilePage = filterContext.ActionDescriptor.ActionName == actionOfProfilePage
&& filterContext.ActionDescriptor.ControllerDescriptor.ControllerName == controlerOfProfilePage;

if (areWeAlreadyInProfilePage) //this will prevent redirect loop
return;

bool isProfileComplete = false; //replace this with your custom logic

if (!isProfileComplete)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", controlerOfProfilePage },
{ "action", actionOfProfilePage }
});
}
}
}

要启用它,只需将其添加到 FilterConfig.cs

filters.Add(new ProfileCompletionCheckAttribute());

关于c# - MVC 5 Identity 2.0 在登录时显示配置文件完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23720394/

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