2016-11-07 45 views
2

我在使用ASP.NET Core和Azure AD B2C時,在使用GitHub(active-directory-dotnet-webapp-openidconnect-aspnetcore-b2c)的代碼示例時,註銷部分不起作用。 在帳戶控制器中註銷Azure AD B2C和ASP.NET Core時的問題

 [HttpGet] 
    public async Task LogOff() 
    { 
     if (HttpContext.User != null && HttpContext.User.Identity.IsAuthenticated) 
     { 
      string scheme = (HttpContext.User.FindFirst("http://schemas.microsoft.com/claims/authnclassreference"))?.Value; 
      await HttpContext.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); 
      await HttpContext.Authentication.SignOutAsync(scheme.ToLower(), new AuthenticationProperties { RedirectUri = "/" }); 
     } 
    } 

該方案返回空值。我找不到正確註銷的方法。任何幫助將不勝感激。

回答

1

您的代碼正在尋找「acr」聲明,顯然未找到聲明。 打開Azure門戶並導航到您的Azure AD B2C,然後檢查中的「令牌,會話和SSO配置」部分,其中每個都包含您的登錄/註冊策略。 在令牌兼容性設置,應具有如所示的下列開關:如果開關已經TFP選擇(這是默認的位置)

enter image description here

,所述身份驗證令牌將不包含「ACR」要求,因此在登錄後它不會被添加到ClaimPrincipal對象的聲明集合中,並且隨後將無法用LogOff方法中的代碼訪問。 希望這有助於。

+0

這個答案有效,但我認爲更好的解決方案是修改索賠搜索以找到「tfp」策略ID。 –

0

亞歷克斯是正確的。如果你不修改你的代碼,你應該改變表示爲「acr」。

但是「acr」僅用於向後兼容性,Microsoft建議應該使用「tfp」。 Azure Active Directory B2C: Token, session and single sign-on configuration

我建議您修改示例代碼,如下所示。我也submitted a pull request的原作者與類似的代碼。

 if (HttpContext.User != null && HttpContext.User.Identity.IsAuthenticated) 
     { 
      // try to find the tfp policy id claim (default) 
      var scheme = (HttpContext.User.FindFirst("tfp"))?.Value; 

      // fall back to legacy acr policy id claim 
      if (string.IsNullOrEmpty(scheme)) 
       scheme = (HttpContext.User.FindFirst("http://schemas.microsoft.com/claims/authnclassreference"))?.Value; 

      await HttpContext.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); 
      await HttpContext.Authentication.SignOutAsync(scheme.ToLower(), new AuthenticationProperties { RedirectUri = "/" }); 
     } 
+0

正確。最好轉換爲「tfp」聲明。 我傾向於不喜歡短命名的唯一東西是缺乏標準化的URI,但這更多的是個人偏好。 –