2017-10-04 4 views
0

Il existe de nombreux exemples d'annulation de licences persistantes lors de la déconnexion à l'aide d'IdentityServer4, mais tous affichent le paramètre ClientId de façon statique. Tout le monde sait comment obtenir dynamiquement le ClientId car je prévois d'utiliser ce IdentityServer avec plusieurs clients différents. Voici le code found here vous trouvez habituellement, regardez la deuxième à la dernière ligne:Déconnexion IDSRV4 - obtention dynamique de l'ID client

[HttpPost] 
[ValidateAntiForgeryToken] 
[AllowAnonymous] 
public async Task<IActionResult> Logout(LogoutViewModel model) 
{ 
    var idp = User?.FindFirst(JwtClaimTypes.IdentityProvider)?.Value; 
    var subjectId = HttpContext.User.Identity.GetSubjectId(); 

    if (idp != null && idp != IdentityServerConstants.LocalIdentityProvider) 
    { 
     if (model.LogoutId == null) 
     { 
      // if there's no current logout context, we need to create one 
      // this captures necessary info from the current logged in user 
      // before we signout and redirect away to the external IdP for signout 
      model.LogoutId = await _interaction.CreateLogoutContextAsync(); 
     } 

     string url = "/Account/Logout?logoutId=" + model.LogoutId; 
     try 
     { 
      // hack: try/catch to handle social providers that throw 
      await HttpContext.Authentication.SignOutAsync(idp, new AuthenticationProperties { RedirectUri = url }); 
     } 
      catch(NotSupportedException) 
     { 
     } 
    } 

    // delete authentication cookie 
    await _signInManager.SignOutAsync(); 

    // set this so UI rendering sees an anonymous user 
    HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity()); 

    // get context information (client name, post logout redirect URI and iframe for federated signout) 
    var logout = await _interaction.GetLogoutContextAsync(model.LogoutId); 

    var vm = new LoggedOutViewModel 
    { 
     PostLogoutRedirectUri = logout?.PostLogoutRedirectUri, 
     ClientName = logout?.ClientId, 
     SignOutIframeUrl = logout?.SignOutIFrameUrl 
    }; 

    await _persistedGrantService.RemoveAllGrantsAsync(subjectId, "angular2client"); 

    return Redirect(Config.HOST_URL + "/index.html"); 
} 
+0

Demandez-vous comment trouver l'identifiant client pour l'application cliente qui a déclenché la déconnexion? Aussi, d'où avez-vous eu ce code? –

+1

@ScottBrady Hé, c'est le fameux Scott Brady! Merci d'avoir répondu, je viens de trouver la réponse c'était trop simple. Je n'aurais probablement pas dû poster ça. Je suis sur le point de répondre à ma propre question après le test. Je vais ajouter le lien à ma question. – Helzgate

Répondre

0

C'est ce que je fini par utiliser:

/// <summary> 
    /// Handle logout page postback 
    /// </summary> 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public async Task<IActionResult> Logout(LogoutInputModel model) 
    { 
     var vm = await _account.BuildLoggedOutViewModelAsync(model.LogoutId); 

     if (vm.TriggerExternalSignout) 
     { 
      string url = Url.Action("Logout", new { logoutId = vm.LogoutId }); 
      try 
      { 
       // hack: try/catch to handle social providers that throw 
       await HttpContext.Authentication.SignOutAsync(vm.ExternalAuthenticationScheme, 
        new AuthenticationProperties { RedirectUri = url }); 
      } 
      catch (NotSupportedException) // this is for the external providers that don't have signout 
      { 
      } 
      catch (InvalidOperationException) // this is for Windows/Negotiate 
      { 
      } 
     } 

     // delete local authentication cookie 
     await HttpContext.Authentication.SignOutAsync(); 

     var user = await HttpContext.GetIdentityServerUserAsync(); 
     if (user != null) 
     { 
      await _persistedGrantService.RemoveAllGrantsAsync(user.GetSubjectId(), vm.ClientName); 
      await _events.RaiseAsync(new UserLogoutSuccessEvent(user.GetSubjectId(), user.GetName()));     
     } 

     return View("LoggedOut", vm); 
    }