2016-11-07 47 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,但这更多的是个人偏好。 –