1

我正在使用IdSrv3進行身份驗證。我需要在我的web api owin客戶端中獲取access_token,以在另一個web api客戶端傳遞承載認證。 我Startup.cs代碼:在web api客戶端訪問令牌客戶端IdSrv3身份驗證

public class Startup 
{ 
     public void Configuration(IAppBuilder app) 
     { 

      ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; 

      JwtSecurityTokenHandler.InboundClaimTypeMap.Clear(); 

      var identityServerPath = ConfigurationManager.AppSettings["IdentityServerPath"]; 

      app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
      { 
       Authority = $"{identityServerPath}/core", 

       RequiredScopes = new[] { "openid"}, 
       AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active, 

       // client credentials for the introspection endpoint 
       ClientId = "someid" 
      }); 
     } 
} 

我想通過這種方式來獲得訪問令牌:

var claims = (User as ClaimsPrincipal).Claims; 
var AccessToken = claims.First(x => x.Type == "access_token").Value; 

如何獲得ACCESS_TOKEN?聲明變量是空的。

+0

在那裏有問題嗎? :) –

+0

@JohnKorsnes修正:) –

+0

酷:)請爲下一個開發人員提供一個答案,如果它可以使其他人受益:) –

回答

2

您可以將「PreserveAccessToken」屬性設置爲true(默認爲false)。例如:

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
{ 
    Authority = "https://localhost:44331", 
    ClientId = "apiOne",  
    ClientSecret = "secret", 
    RequiredScopes = new[] {"apiOne"}, 
    ValidationMode = ValidationMode.ValidationEndpoint, 
    PreserveAccessToken = true 
}); 

這會將訪問令牌保留爲聲明。然後,您可以像上面那樣進行檢索。除此之外,聲明是「標記」,而不是「access_token」。

+0

它再次爲空:( –

+0

@AstemirAlmov - 那麼,根本沒有聲明?可以發佈顯示其他作品配置的代碼嗎?例如,客戶端調用該Web API和該客戶端的IdentityServer配置? –