简体   繁体   English

如何确保用户在aspnet core 2.0中完成他们的个人资料?

[英]How to make sure a user completes their profile in aspnet core 2.0?

I'm new to aspnet core 2.0 and what I would like to accomplish is to redirect a user who has not completed their profile to a page that they can do so. 我是aspnet核心2.0的新手,我想要完成的是将未完成个人资料的用户重定向到他们可以这样做的页面。 I'm using the default template with user authentication for Identity server. 我正在使用默认模板和身份服务器的用户身份验证。

I have tried using middleware but the page that i redirect to comes out blank. 我尝试过使用中间件但是我重定向的页面是空白的。 Is this the best approach or can someone help make it work. 这是最好的方法还是有人可以帮助它发挥作用。 here is my middleware. 这是我的中间件。

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);
        }
    }
}

Thanks in advance. 提前致谢。

Just looking at the current logic in your code I notice that if the user is not null and has its profile completed will short circuit the call. 只是查看代码中的当前逻辑,我注意到如果用户不为null并且其配置文件已完成,则会使调用短路。 You need to add in that logic. 你需要添加那个逻辑。

Try the following 请尝试以下方法

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM