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

C# MVC 5 Identity 2.0登录时显示配置文件完成,c#,asp.net-mvc-5,asp.net-identity,C#,Asp.net Mvc 5,Asp.net Identity,使用MVC 5和Identity 2.0,我将自定义属性添加到ApplicationUserClass中,如FirstName,LastName,Address。这些将是数据库中的新字段。当用户注册到应用程序时,他/她将只输入电子邮件地址和密码。在他们注册并登录之后,我想强制他们完成他们的个人资料,或者至少在每次登录时,他们应该被重定向到个人资料完成页面,在那里他们可以提到名字、姓氏和地址。完成配置文件后,他们不会在每次登录时重定向到完成配置文件页面 比如: if UserProfile !=

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

比如:

if UserProfile != Completed

   go to CompleteProfilePage

else

   go to MainPage

在您的AccountController中,类似以下内容:

//
// POST: /Account/Login

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{

    if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
    {
        return RedirectToLocal(returnUrl);
    }


    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return View(model);
}
像这样升级

//
// POST: /Account/Login

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
    {

        UsersContext dbu = new UsersContext();
        UserProfile usr = dbu.UserProfiles.Where(u => u.UserName == model.UserName).First();

        if (usr.FirstName == null) return RedirectToAction("Profile", "Account");

        return RedirectToLocal(returnUrl);

    }

    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return View(model);
}

您可以尝试使用全局筛选器。这将不允许用户通过手动修改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(新ProfileCompletionCheckAttribute())

我在想,无论用户进入哪个页面,我都需要在应用程序中全局实现自定义筛选器,因此我基本上必须获取当前用户,然后检查每个属性是否包含数据,并相应地设置isProfileComplete,对吗?