2017-10-17 124 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。這工作完美。 –

相關問題