2017-10-17 126 views
3

我是新来的aspnet核心2.0,我想完成的是将未完成其个人资料的用户重定向到他们可以这样做的页面。我正在使用Identity Server的用户身份验证默认模板。如何确保用户在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); 
     } 
    } 
} 

在此先感谢。

+0

只要看看在你的代码目前的逻辑,我注意到,如果用户不为空,有其轮廓建成后将短路呼叫。您需要添加该逻辑。这可能是X-Y问题 – Nkosi

+0

Hi @Nkosi,感谢您的快速回复。我已经删除了else条件,并在if语句之后调用了'await next(context);'我仍然得到一个空白页面。 –

回答

3

只看代码中的当前逻辑我注意到,如果用户不为空,并且其配置文件完成将短路呼叫。您需要添加该逻辑。

请尝试以下

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

谢谢@Nkosi。这工作完美。 –

相关问题