gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:56:27 25 4
gpt4 key购买 nike

我是 aspnet core 2.0 的新手,我想完成的是将尚未完成个人资料的用户重定向到他们可以完成个人资料的页面。我正在使用带有身份服务器用户身份验证的默认模板。

我试过使用中间件,但我重定向到的页面是空白的。这是最好的方法还是有人可以帮助它工作。这是我的中间件。

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

提前致谢。

最佳答案

只要查看您代码中的当前逻辑,我就会注意到,如果用户不为 null 并且其配置文件已完成,则会使调用短路。您需要添加该逻辑。

尝试以下操作

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

关于c# - 如何确保用户在 aspnet core 2.0 中完成他们的个人资料?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46792453/

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