代码之家  ›  专栏  ›  技术社区  ›  NIMROD MAINA

如何确保用户在aspnet core 2.0中完成其配置文件?

  •  3
  • NIMROD MAINA  · 技术社区  · 8 年前

    我刚接触过aspnet core 2.0,我想完成的是将尚未完成个人资料的用户重定向到他们可以完成的页面。我正在使用默认模板进行Identity server的用户身份验证。

    我曾尝试使用中间件,但我重定向到的页面显示为空白。这是最好的方法吗?或者有人能帮助它工作吗。这是我的中间件。

    public class FarmProfileMiddleware
    {
        private readonly RequestDelegate next;
    
        public FarmProfileMiddleware(RequestDelegate next)
        {
            this.next = next;
    
        }
    
        public async Task Invoke(HttpContext context, UserManager<ApplicationUser> userManager)
        {
            var user = await userManager.GetUserAsync(context.User);
            if (user != null)
            {
                if (!user.EmailConfirmed && !context.Request.Path.ToString().Contains("profile"))
                {
                    var url = context.Request.PathBase + "/Users/Profile";
                    context.Response.Redirect(url);    
                }
            }
            else
            {
                await next(context);
            }
        }
    }
    

    提前谢谢。

    1 回复  |  直到 8 年前
        1
  •  3
  •   Nkosi    8 年前

    尝试以下操作

    var user = await userManager.GetUserAsync(context.User);
    if (user != null && !user.EmailConfirmed && !context.Request.Path.ToString().Contains("profile")) { 
        var url = context.Request.PathBase + "/Users/Profile";
        context.Response.Redirect(url);            
    } else {
        await next(context);
    }